iOS 应用程序在后台时的 GCM 推送通知 [英] GCM push notification when iOS app is in the background

查看:37
本文介绍了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"
  }
}

这篇关于iOS 应用程序在后台时的 GCM 推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆