简体   繁体   English

如果将“数据”(但“通知”工作)有效负载发送到iOS中的GCM / FCM,则不会收到推送通知didReceiveRemoteNotification

[英]Not receiving push notifications if sending “data” (but “notification” works) payloads to GCM/FCM in iOS didReceiveRemoteNotification

I am trying to get "data" payload notifications to be received for our iOS app. 我正在尝试为我们的iOS应用程序收到“数据”有效负载通知。

Today we can send GCM notification push notifications as according to: 今天我们可以按照以下方式发送GCM notification推送通知:

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

(FCM has the same text) (FCM有相同的文字)

An easy test is using CURL: 一个简单的测试是使用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_##"
}
'

This will successfully trigger iOS AppDelegate.didReceiveRemoteNotification:fetchCompletionHandler function. 这将成功触发iOS AppDelegate.didReceiveRemoteNotification:fetchCompletionHandler函数。

However, if change it to a data notification: 但是,如果将其更改为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_##"
}
'

I can't see anything is being sent to the app from GCM (in either didReceiveRemoteNotification functions), even if the app is in background/foreground. 即使应用程序处于后台/前台,我也看不到任何内容从GCM发送到应用程序(在didReceiveRemoteNotification函数中)。

Even though it says in the documentation it should: 即使它在文档中说它应该:

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

Note these further platform-specific details: 请注意这些进一步的平台特定细节:

  • On Android, data payload can be retrieved in the Intent used to launch your activity. 在Android上,可以在用于启动活动的Intent中检索数据有效负载。
  • On iOS, data payload will be found in didReceiveRemoteNotification:. 在iOS上,数据有效负载将在didReceiveRemoteNotification中找到:

GCM can handle pure data push notifications to the APN network right? GCM可以处理到APN网络的纯data推送通知吗?

Do I need to do anything special to receive data , compared to notification , Push Notifications in iOS? notification ,iOS中的推送通知相比,我是否需要做一些特殊的接收data

When sending the Data type message with FCM to iOS devices, they will only be received if content_available is set to true in your FCM request body, eg: 将带有FCM的数据类型消息发送到iOS设备时,只有在FCM请求正文中将content_available设置为true时才会收到它们,例如:

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

Aside from the notes that you've shared, please don't miss out that, 除了您分享的笔记外,请不要错过,

On iOS, GCM stores the message and delivers it only when the app is in the foreground and has established a GCM connection. 在iOS上,GCM存储消息并仅在应用程序位于前台并建立GCM连接时传递消息。

With this, you may want to check Establishing a Connection . 有了这个,您可能需要检查建立连接 Then, when your XMPP connection is established, CCS and your server use normal XMPP <message> stanzas to send JSON-encoded messages back and forth. 然后,当您建立XMPP连接时,CCS和您的服务器使用普通的XMPP <message>节来回发送JSON编码的消息。 The body of the <message> must be: <message>的正文必须是:

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

Also, note that message_id is a required field for data message. 另请注意, message_id是数据消息的必填字段。 Check this sample request format for message with payload - data message shown in Downstream Messages . 检查带有有效负载的消息的此样本请求格式 - 下游消息中显示的数据消息。 You just have to convert it using CURL. 你只需要使用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>

For more information, see XMPP Connection Server Reference . 有关更多信息,请参阅XMPP连接服务器参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM