【问题标题】:Not receiving push notifications if sending "data" (but "notification" works) payloads to GCM/FCM in iOS didReceiveRemoteNotification如果在 iOS didReceiveRemoteNotification 中向 GCM/FCM 发送“数据”(但“通知”有效)有效负载,则不接收推送通知
【发布时间】:2017-12-04 10:39:30
【问题描述】:

我正在尝试为我们的 iOS 应用获取“数据”有效负载通知。

今天我们可以按照以下方式发送 GCM notification 推送通知:

https://developers.google.com/cloud-messaging/concept-options

(FCM有相同的文字)

一个简单的测试是使用 CURL:

curl -X POST \
  https://gcm-http.googleapis.com/gcm/send \
  -H 'authorization: key=##_GCM_SERVER_ID_##' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'postman-token: ##_POSTMAN_TOKEN_##' \
  -d '{
    "notification": {
        "body": "Test body"
    },
    "to" : "##_DEVICE_TOKEN_##"
}
'

这将成功触发iOSAppDelegate.didReceiveRemoteNotification:fetchCompletionHandler函数。

但是,如果将其更改为data 通知:

curl -X POST \
  https://gcm-http.googleapis.com/gcm/send \
  -H 'authorization: key=##_GCM_SERVER_ID_##' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'postman-token: ##_POSTMAN_TOKEN_##' \
  -d '{
    "data": {
        "body": "Test body"
    },
    "to" : "##_DEVICE_TOKEN_##"
}
'

我看不到任何东西从 GCM 发送到应用程序(在 didReceiveRemoteNotification 函数中),即使应用程序处于后台/前台。

即使它在文档中说它应该:

https://developers.google.com/cloud-messaging/concept-options#notifications_and_data_messages

请注意这些平台特定的详细信息:

  • 在 Android 上,可以在用于启动 Activity 的 Intent 中检索数据负载。
  • 在 iOS 上,数据负载将在 didReceiveRemoteNotification: 中找到。

GCM 可以处理纯data 向 APN 网络推送通知,对吗?

notification 相比,iOS 中的推送通知我是否需要做一些特别的事情才能接收data

【问题讨论】:

标签: ios notifications google-cloud-messaging firebase-cloud-messaging


【解决方案1】:

使用 FCM 向 iOS 设备发送数据类型消息时,只有在 FCM 请求正文中将 content_available 设置为 true 时才会收到它们,例如:

{
    "to": "--fcm-token--",
    "content_available": true,
    "data": {
        "priority": "high",
        "hello": "world"
    }
}

【讨论】:

  • "content_available" 是这里的关键!
【解决方案2】:

除了您分享的笔记,请不要错过,

在 iOS 上,GCM 仅在应用处于前台并已建立 GCM 连接时存储消息并传递消息。

有了这个,你可能想检查Establishing a Connection。然后,当您的 XMPP 连接建立时,CCS 和您的服务器使用普通的 XMPP <message> 节来回发送 JSON 编码的消息。 <message> 的正文必须是:

<gcm xmlns:google:mobile:data>
    JSON payload
</gcm>

另外,请注意message_id 是数据消息的必填字段。检查此示例请求格式以获取带有有效负载的消息 - Downstream Messages 中显示的数据消息。您只需使用 CURL 进行转换。

<message id="">
  <gcm xmlns="google:mobile:data">
  {
      "to":"REGISTRATION_ID",  // "to" replaces "registration_ids"
      "message_id":"m-1366082849205" // new required field
      "data":
      {
          "hello":"world",
      }
      "time_to_live":"600",
      "delay_while_idle": true/false,
      "delivery_receipt_requested": true/false
  }
  </gcm>
</message>

如需了解更多信息,请参阅XMPP Connection Server Reference

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-14
    • 1970-01-01
    • 2016-07-19
    • 2017-05-31
    • 1970-01-01
    • 2021-04-26
    • 2014-02-12
    • 1970-01-01
    相关资源
    最近更新 更多