我正在尝试使用 GCM 向我的 iOS 应用程序发送推送通知。该应用程序在后台时不会收到通知,但在前台时会收到通知。我正在使用 PHP 脚本测试推送通知,该脚本还将消息直接发送到 APNS 并且它在后台运行。
发送到 GCM 的 JSON:(我从休息客户端发送它用于测试)
{
"to" : "token...",
"notification" : {
"title": "GCM TITLE",
"body" : "FROM GCM",
"badge": "1",
"sound": "default"
}
}
不工作: 在 didReceiveRemoteNotification 中从 GCM 收到的 userInfo:
Notification received: [aps: {
alert = {
body = "FROM GCM";
title = "GCM TILE";
};
badge = 1;
sound = default;
}, gcm.message_id: 123...]
工作: 从 PHP 脚本发送时收到的 userInfo(我还将 message_id 添加到 JSON 以查看是否是问题所在)
Notification received: [aps: {
alert = {
body = "FROM PHP";
title = "PHP TITLE";
};
badge = 2;
sound = default;
}, gcm.message_id: 123...]
我尝试将 content_available 添加到具有不同组合的 JSON 但没有帮助,还设置了 Content-Type 和 Authorization 请求 header :
Content-Type:application/json
Authorization:key=...
最佳答案
万一有人遇到同样的问题,我的解决方案是将 "priority": "high"添加到 JSON。这样我就可以在后台收到通知。
{
"to" : "token...",
"priority": "high",
"notification" : {
"title": "GCM TITLE",
"body" : "FROM GCM",
"badge": "1",
"sound": "default"
}
}
关于ios - iOS 应用程序在后台时的 GCM 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33192145/