iOS 应用程序在后台时的 GCM 推送通知

iOS 应用程序在后台时的 GCM 推送通知

问题描述:

我正在尝试使用 GCM 向我的 iOS 应用程序发送推送通知.该应用程序在后台时不会收到通知,但在前台时会收到通知.我正在使用 PHP 脚本测试推送通知,该脚本也将消息直接发送到 APNS 并且它在后台运行.

I'm trying to send push notifications to my iOS app with GCM. The app doesn't get the notification when it's in the background but it does when it's in the foreground. I was testing the push notifications with a PHP script also which sends the message directly to the APNS and it's working in the background.

发送到 GCM 的 JSON:(我是从其他客户端发送的用于测试)

The JSON sent to GCM: (I'm sending it from a rest client for testing)

{
  "to" : "token...",
  "notification" : {
    "title": "GCM TITLE",
    "body" : "FROM GCM",
    "badge": "1",
    "sound": "default"
  }
}

不工作:在 didReceiveRemoteNotification 中从 GCM 收到的 userInfo:

Not working: The userInfo received from GCM in didReceiveRemoteNotification:

Notification received: [aps: {
    alert =     {
        body = "FROM GCM";
        title = "GCM TILE";
    };
    badge = 1;
    sound = default;
}, gcm.message_id: 123...]

工作:从 PHP 脚本发送时收到的 userInfo(我还在 JSON 中添加了 message_id 以查看是否有问题)

Working: The userInfo received when sent from the PHP script (I also added the message_id to the JSON to see if that's the problem)

Notification received: [aps: {
    alert =     {
        body = "FROM PHP";
        title = "PHP TITLE";
    };
    badge = 2;
    sound = default;
}, gcm.message_id: 123...]

我尝试将 content_available 添加到具有不同组合的 JSON 但没有帮助,还设置了 Content-Type 和 Authorization 请求标头:

I tried adding content_available to the JSON with different combinations but didn't help, the Content-Type and Authorization request headers are also set:

Content-Type:application/json
Authorization:key=... 

如果有人遇到同样的问题,我的解决方案是将 "priority": "high" 添加到 JSON.这样我就可以在后台收到通知.

In case if someone has the same problem, the solution was for me to add the "priority": "high" to the JSON. This way I get the notifications in the background.

{
  "to" : "token...",
  "priority": "high",
  "notification" : {
    "title": "GCM TITLE",
    "body" : "FROM GCM",
    "badge": "1",
    "sound": "default"
  }
}