我们聘请了一名远程开发人员来将推送通知添加到我们的 iOS 应用程序中。我们已经让它在 Android 上运行,所以我们知道服务或我们的 API 没有问题,但开发人员坚持认为我们需要更改进入我们端 ios 应用程序的消息。
这是 php:
private function sendNotifications($recipients, $type, $id) {
require_once 'aws.phar';
try{
$client = Aws\Sns\SnsClient::factory(array(
'region' => '#######',
'version' => '###########',
'credentials' => [
'key' => '##################',
'secret' => '####################################'
],
'scheme' => 'http'
));
foreach($recipients as $recipient) {
$topicName = $recipient;
$topicArn = '####################################'.$topicName;
$alert = "Somebody just left you a message";
if($type == "like") {
$alert = "Somebody liked your message";
}else if($type == "reply") {
$alert = "Somebody replied to your message";
}
$message = '
{
"type" : "' . $type . '",
"id" : "' . $id . '"
}
';
try{
$result = $client->publish(array(
'TopicArn' => $topicArn,
'Message' => $message
));
}catch(Exception $e2) {
// Topic doesn't exist
}
}
}catch(Exception $e) {
$this->log($e->getMessage());
}
}
这是我要求他发送给我的片段:
// Push notification received
func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
let aps = data[AnyHashable("aps")] as? NSDictionary
let alert = aps?.value(forKey: "alert") as? String
print(alert)
let alert2 = convertToDictionary(text: alert!)
let type = alert2!["type"] as? String
let id = alert2!["id"] as? String
var messageAlert = ""
if type != nil {
switch type! {
case "message":
messageAlert = "Somebody just messaged you"
case "reply":
messageAlert = "Somebody replied to your message"
case "like":
messageAlert = "Somebody liked your message"
default:
messageAlert = "Somebody just messaged you"
}
}
UserDefaults.standard.set(id, forKey: "MessageIDFromNotification")
UserDefaults.standard.synchronize()
NotificationCenter.default.post(name: Notification.Name("UserNotification"), object: nil)
NotificationCenter.default.post(name: Notification.Name("openedFromNotification"), object: nil)
if application.applicationState == .inactive || application.applicationState == .background {
if((UserDefaults.standard.value(forKey: USER_DEFAULTS_USERTOKEN) as? String) != nil)
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = self.window?.rootViewController as? UINavigationController
let tabbar = storyboard.instantiateViewController(withIdentifier: "AMTabBarVC") as! AMTabBarVC
tabbar.selectedIndex = 0
tabbar.InitialVCindex = 0
tabbar.selectBackgroundForItem(at: 0, backgroundColor: UIColor.red, withEarlySelect: true, animated: false)
navigationController?.pushViewController(tabbar, animated: true)
}
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Convert token to string
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
UserDefaults.standard.set(deviceTokenString, forKey: "deviceToken")
UserDefaults.standard.synchronize()
print("Device Token Registered: \(deviceTokenString)")
}
我不熟悉 Swift,所以我不确定这应该是什么样子,但开发人员坚持:
现在您正在发送:
{ “aps”:{ "alert": "{\n\"type\":\"message\",\n\"id\":\"109556\",\n\"alert\":\"有人刚刚给你发了消息\"\n}", “声音”:“默认” } }
但您需要发送如下内容:
{ “aps”:{ "alert": "有人刚刚给你发消息", “声音”:“默认”, “类型”:“消息”, “id”:“109556” } },
当我问他为什么不能直接解析 json 时,他回答道:
对于 iOS,默认情况下您在警报中传递的任何信息都会显示在通知中。 我们无法像 android 那样预处理通知。作为对 iOS 的限制
这个问题可以在 swift 代码中修复吗?整件事让我感觉很奇怪......
最佳答案
您的开发人员是正确的 - Apple 正在寻找特定的按键来处理推送通知。有效负载可以被解析以获取用户定义的键和值并做出相应的 react ,但是诸如警报
、声音
、等基本内容>徽章
需要按照开发人员告诉您的方式进行构建,以便手机可以在应用程序处于后台时向您的用户显示它们。
Apple 的 Local and Remote Notification Programming Guide 中的完整详细信息.
关于php - 快速解析AWS推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48197436/