如果在 iOS didReceiveRemoteNotification 中向 GCM/FCM 发送“数据”(但“通知”有效)有效负载,则不接收推送通知

cor*_*ath 5 notifications ios google-cloud-messaging firebase-cloud-messaging

我正在尝试为我们的 iOS 应用程序接收“数据”有效负载通知。

今天,我们可以notification根据以下内容发送 GCM推送通知:

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_##"
}
'
Run Code Online (Sandbox Code Playgroud)

这将成功触发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_##"
}
'
Run Code Online (Sandbox Code Playgroud)

didReceiveRemoteNotification即使应用程序在后台/前台,我也看不到任何东西从 GCM 发送到应用程序(在任一功能中)。

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

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

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

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

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

datanotificationiOS 中的推送通知相比,我是否需要做任何特别的事情来接收?

Tey*_*yam 0

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

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

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

<gcm xmlns:google:mobile:data>
    JSON payload
</gcm>
Run Code Online (Sandbox Code Playgroud)

另请注意,这message_id是数据消息的必填字段。检查此示例请求格式以获取具有下游消息中所示有效负载数据消息的消息。您只需使用 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>
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅XMPP 连接服务器参考