Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 948
corgrath Avatar asked Oct 18 '22 10:10

corgrath


1 Answers

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"
    }
}
like image 132
Ric Santos Avatar answered Nov 03 '22 01:11

Ric Santos