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

我正尝试使用GCM将推送通知发送到我的iOS应用程序。 应用程序在后台收到通知时不会收到通知,但会在前台收到通知。 我正在使用PHP脚本testing推送通知,该脚本也将消息直接发送到APNS,并在后台工作。

发送给GCM的JSON :(我从一个rest客户端发送来进行testing)

{ "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请求头也被设置:

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

如果有人遇到同样的问题,我的解决scheme是为JSON添加“优先级”:“高”。 这样我在后台得到通知。

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

要获得应用程序在后台的通知我注意到我们需要添加:

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