Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Using data only in push payload is not valid on all platforms #1828

Open
MarsupiL opened this issue Jul 5, 2017 · 10 comments
Open

Using data only in push payload is not valid on all platforms #1828

MarsupiL opened this issue Jul 5, 2017 · 10 comments
Labels

Comments

@MarsupiL
Copy link

MarsupiL commented Jul 5, 2017

Expected Behaviour

According to the plugin doc:
...my recommended format for your push payload when using this plugin (while it differs from Google's docs) works 100% of the time

{
    "data" : {
        "title": "Test Notification",
        "body": "This offer expires at 11:30 or whatever",
        "notId": 10,
        "surveyID": "ewtawgreg-gragrag-rgarhthgbad"
    }
}

Actual Behaviour

Having both notification and data is the only way I managed to get the notifications to open the app on both platforms. But the on('notification') handler is never called in Android and is randomly called in iOS.
Having all data under data does not trigger any notification in iOS even though Firebase returns a successful request because the forwarded APNS payload is not correct.
Besides the proper way should be to follow the GCM or FCM payload recommendations, which is to use both notification for the notification message and data for custom key/value pairs. Like so:

{
    "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
    "notification" : {
      "body" : "great match!",
      "title" : "Portugal vs. Denmark",
      "icon" : "myicon"
    },
    "data" : {
      "Nick" : "Mario",
      "Room" : "PortugalVSDenmark"
    }
  }

Sample requests to reproduce:

curl -X POST -H "Authorization: key=AAAAxxx" -H "Content-Type: application/json" -d '{"to":"cw4Rxxx","data":{"title":"Hi","body":"my message","id":"edfxxx"},content_available":true}' "https://fcm.googleapis.com/fcm/send"

or

curl -X POST -H "Authorization: key=AAAAxxx" -H "Content-Type: application/json" -d '{"to":"cw4Rxxx","notification":{"title":"Hi","body":"my message"},"data":{"id":"edfxxx"},"content_available":true}' "https://fcm.googleapis.com/fcm/send"

I see through other issues in this plugin that we should use different payloads for the different platforms. This adds too much complexity on the server side and is again defeating the purpose of GCM/FCM that do work with a single payload for both platforms.
Could you please confirm that this is indeed a bug and whether it will be fixed in the next stable FCM version of this plugin?
Thanks for the effort.

cordova 6.5.0
cordova android 6.2.2
cordova ios 4.4.0
phonegap-plugin-push 1.10.5

@fredgalvao
Copy link
Collaborator

Although you're correct in interpreting that chunk of text from the docs by itself, there is context around it: it applies only to the Android section of the payload docs.

So, you should read (or we should reword it to be more readable):

...my recommended format for your push payload when using this plugin (while it differs from Google's docs) works 100% of the time ON ANDROID

@fredgalvao
Copy link
Collaborator

There is no way as of right now, to specify a single payload that works 100% on both/all platforms. You'll need to differ your users/targets on the server, and send a separate payload for each of them (data for android, notification for iOS).

@macdonst macdonst added the docs label Jul 5, 2017
@macdonst
Copy link
Member

macdonst commented Jul 5, 2017

@MarsupiL Yeah, the problem on Android is the way Google sets up when the onMessageReceived handler is called. I try to document it here: https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#notification-vs-data-payloads

You will need to segment your pushes via the Android, iOS lines in order for this to work. It's really not that difficult on the server side to do this you just need to let your server know if the device is Android or iOS when you send it the registration token.

@MarsupiL
Copy link
Author

MarsupiL commented Jul 6, 2017

So I assume from the responses that the 2 different payloads will still be needed in v2 of this plugin using FCM, correct?

Maybe it'd help to clarify this in the overview part of the Payload doc.
And I would also add an example of 2 typical payloads for both platforms, like so:

Android payload example:
{
  "to":"fcm-registration-token", 
  "data":
    {
      "title":"My title", 
      "body":"My message", 
      "image":"https://image.url.com/image.png", 
      "image-type":"circle",
      "sound":"default", 
      "badge": "0", 
      "id":"edfxxx", 
    }
}

and

iOS payload example:
{
  "to":"fcm-registration-token", 
  "notification":
    {
      "title":"My title", 
      "body":"My message", 
      "sound":"default", 
      "badge": "0"
    }, 
  "data":
    {
      "id":"edfxxx"
    }
}

@sawest
Copy link

sawest commented Aug 1, 2017

I am having the same issue. This problem becomes very apparent when you are sending to topics. I want all my platforms to subscribe to a single topic so that I can send a single POST to that topic which should send a notification to any platform subscribed. However, I cannot send one POST that iOS and Android both can use. I also cannot send only to iOS or only to Android when sending to a topic (as both platforms are subscribed).

The only work around for me is to split all my topics up by platform. So instead of having a 'general' topic I will now have a 'General iOS' topic and 'General Android' topic. The device code will need to be smart enough to know which topic to subscribe to. When sending a notification, the notification will need to be sent to both topics now in 2 seperate POSTs which does defeat the purpose of FCM and adds a lot of complexity.

If I was sending to an individual device then this would be easy. Hope this helps give some perspective on how this can be a real issue moving forward.

Appreciate all the work.

@macdonst
Copy link
Member

@MarsupiL that is correct. The 2 different payloads will still be needed.

@sawest I'm aware that this is a real issue. It's been one ever since GCM was supported on iOS. Sadly, there doesn't appear to be anything I can do.

In your app you can use the device plugin to determine which platform you are running on and subscribe the the correct topic.

@mitchtabian
Copy link

So, if I'm understanding this correctly: Using DATA ONLY payloads for Android is best because it will be received in all app states (Background, Foreground and Closed). The developer can then handle the different situations depending on the app state.
Example:
Android payload example:


{
  "to":"fcm-registration-token", 
  "data":
    {
      "title":"My title", 
      "body":"My message", 
      "image":"https://image.url.com/image.png", 
      "image-type":"circle",
      "sound":"default", 
      "badge": "0", 
      "id":"edfxxx", 
    }
}

However, on the iOS side of the world, just data payloads don't work? So you need to include a notification. iOS Example:


{
  "to":"fcm-registration-token", 
  "notification":
    {
      "title":"My title", 
      "body":"My message", 
      "sound":"default", 
      "badge": "0"
    }, 
  "data":
    {
      "id":"edfxxx"
    }
}

So we could probably put a "band-aid" on this issue by sending two separate messages? One with a data only payload and one with a notification payload? The one with the data only payload wouldn't be read by the iOS apps and the one with the notification payload wouldn't be read by Android apps.

Am I understanding this correctly?

Thanks all,
--Mitch

@roxteddy
Copy link

roxteddy commented Nov 18, 2017

Hi there !

Using HTTPv1 FCM API, I manage to handle background notification the right way with both Android and iOS.

  • iOS uses regular 'data' while Android has its own in 'android' section
  • there's no 'notification' section anymore, it is replaced by 'apns > paypload > aps > alert > title and body' so it doesn't mess with Android handler

both on android and ios my event is fired directly when in foreground or it is fired on notification click when in background.

Hope it will inspire you to use this API

(sorry I can't find how to format this json to make it readable)

{
   "validateOnly":false,
   "message":{
      "data":{
         "data1":"value1"
      },
      "android":{
         "data":{
            "data1":"value1",
            "title":"Hello",
            "body":"Please confirm your availability"
         },
         "restricted_package_name":"com.test.app",
         "priority":"high"
      },
      "name":"Planning validation",
      "apns":{
         "payload":{
            "aps":{
               "alert":{
                  "title":"Hello",
                  "body":"Please confirm your availability"
               }
            }
         }
      },
      "token":"[valid FCM token]"
   }
}

@fredgalvao
Copy link
Collaborator

@roxteddy Try surrounding your code block with isolated triple backticks. I edited your comment to make it so.
Check here for more info on how to do that.

@stale
Copy link

stale bot commented Jun 3, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jun 3, 2018
@macdonst macdonst removed the wontfix label Jun 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

6 participants