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

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

不工作: 在 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 请求 header :

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

最佳答案

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

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

关于ios - iOS 应用程序在后台时的 GCM 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33192145/

相关文章:

android - 内部错误 : retry receiver class not set yet

ios - 如何保持 Tableview 滚动以及如何在 Swift 3 中删除原型(prototype)单元格和 tableview 之间的空白空间

Android - 显示没有默认通知的自定义通知 - Parse.com

compiler-errors - 每10分钟发送一次电子邮件通知

Firebase 在单个请求中发送多个通知

java - 来自 GCM "no empty constructor"错误的 Android 下游消息

android - 如何在没有 gcm 注册的情况下解析发送推送?

ios - Xcode 编译标志以抑制可空性警告不起作用?

ios - 在反向 Segue 操作中,选项卡栏在选项卡栏 Controller 上不可见

ios - 方法被调用,但未正确执行