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

时间:2015-10-17 22:14:24

标签: ios notifications google-cloud-messaging apple-push-notifications

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

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

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

无效: userInfo在didReceiveRemoteNotification中从GCM收到:

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请求标头:

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

2 个答案:

答案 0 :(得分:8)

如果有人遇到同样的问题,解决办法是让我添加"优先级":"高"到JSON。这样我就可以在后台收到通知。

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

答案 1 :(得分:0)

要在应用处于后台时收到通知,请注意我们需要添加:

"content_available":true // when app in background
"priority":"high" //when app is completely closed not even running in background

 // "content_available":true  ..most important field 

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