【问题标题】:GCM push notification when iOS app is in the backgroundiOS应用程序在后台时的GCM推送通知
【发布时间】:2016-01-16 11:39:07
【问题描述】:

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

发送到 GCM 的 JSON:(我是从 REST 客户端发送它以进行测试)

{
  "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=... 

【问题讨论】:

  • 可能有效,也可能无效,我遇到了类似的问题,请尝试从 JSON 中删除标题
  • 感谢您的建议!不幸的是它仍然不起作用
  • 您是否从 GCM 获得了成功?你在测试什么环境,你是把应用存档还是直接测试到设备上?
  • 是的,我知道消息已成功传递,我正在我的设备上对其进行测试。但现在它似乎工作了,我在 JSON 中将优先级设置为“高”,现在我在后台收到通知!
  • mmm GCM 为你服务,我花了一段时间才让它完全正常工作,我在几个应用程序中都有它,但似乎总是对 APNS 有点打击或错过。无论如何,很高兴你让它工作了

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-30
      • 1970-01-01
      相关资源
      最近更新 更多