首页 文章

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

提问于
浏览
5

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

发送给GCM的JSON :(我从其他客户端发送它进行测试)

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

Not working :在didReceiveRemoteNotification中从GCM收到的userInfo:

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

Working :从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 回答

  • 8

    如果有人有同样的问题,解决方案是我为JSON添加“优先级”:“高” . 这样我就可以在后台收到通知 .

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

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

    "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"
      }
    }
    

相关问题