简体   繁体   中英

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.

Today we can send GCM notification push notifications as according to:

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

(FCM has the same text)

An easy test is using 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.

However, if change it to a data notification:

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.

Even though it says in the documentation it should:

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.
  • On iOS, data payload will be found in didReceiveRemoteNotification:.

GCM can handle pure data push notifications to the APN network right?

Do I need to do anything special to receive data , compared to notification , Push Notifications in iOS?

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:

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

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. The body of the <message> must be:

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

Also, note that message_id is a required field for data message. Check this sample request format for message with payload - data message shown in Downstream Messages . You just have to convert it using 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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