I faced 2 issues in Android with the latest update to GCM. As per GCM, it will automatically display notification in the tray if the payload contains 'notification' attribute. But they have not mentioned how to handle on tap event for that notification. If the payload contains only data attribute, 'onMessageReceived' of GCMListenerService is invoked. But if the payload contains both notification and data attributes, the method is not invoked. Any idea how to resolve? I have to check iOS as well to see how it behaves there.
-
1Did you try to use Parse.com? It's pretty straightforward for Android and iOS. But I think you want to use only GCM... – Franzé Jr. Jun 11 '15 at 16:21
2 Answers
You need to set a click_action in the notification payload. Then when the user opens/clicks on the notification, an Activity in your app declared with that action will be launched.
e.g set click_action: OPEN_ACTIVITY_1, and add the following intent filter to the desired Activity:
<intent-filter>
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Then you can extract the data from the message in the Activity, by using getIntent() and then looking at the intent extras.
See the entry here: https://developers.google.com/cloud-messaging/server-ref#notification-payload-support

- 973
- 6
- 9
-
Do you think, GcmListenerService is no more useful as its not even called when notification attribute is present in payload? – Winster Jun 12 '15 at 12:46
-
There is no way to send a single message that will show a notification, and invoke the GcmListenerService. You can get the same effect by sending two messages, one with notification payload, and one with only data payload. – morepork Jun 12 '15 at 17:20
-
Don't you need to specify an intent to go to the activity you put the intent-filter inside of? I put that intent-filter inside one of my views and nothing happens when i click on the notification that has "click_action": "OPEN_ACTIVITY_1" in the notification key in the json object. I'm new to Android dev so any suggestions will help. – bxiong Dec 05 '15 at 00:59
-
@WinsterJose, GcmListenerService is still useful because gcm only handles the showing of notification when your app is inactive. Otherwise, onMessageReceived() is still called and it should handle the showing of notification. – vida Mar 11 '16 at 03:39
-
1Only doing this did not worked for me. I needed to add [these codes](http://stackoverflow.com/a/38149137/5528870) to launch the specified activity and finally it worked – Hilal Sep 07 '16 at 11:42
If you sent push with notification
param, then you can't handle by on onMessageReceived
If you want handle the notification, send push with data
option, now you can handle with GCM and onMessageReceived
.
Notice: ios handle notifications with notifications
key.
Send push for ios with notification
key, for android data
key.
For android;
{"data":{"message":"hey"}},registration_ids":["device token"]
For ios:
{"notification":{"title":"Hey title", "body":" Hey body"},"to":"device token"}

- 251
- 2
- 5
-
registration_ids will deliver the message to just one device or to more(since it is an array). How to send it to every device(without actually listing all the ids) on which the app is installed? (in both IOS and Android) – BraveNinja Jun 19 '16 at 23:43