Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle tap of notification in Android if displayed automatically by GCM

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.

like image 540
Winster Avatar asked Jun 11 '15 15:06

Winster


People also ask

How do you handle payload notifications?

Handle notification messages in a backgrounded appA user tap on the notification opens the app launcher by default. This includes messages that contain both notification and data payload (and all messages sent from the Notifications console).

What displays notification messages to the user from within the Android enabled device?

Status bar and notification drawer When you issue a notification, it first appears as an icon in the status bar. Users can swipe down on the status bar to open the notification drawer, where they can view more details and take actions with the notification.

How do I handle the Firebase notification when an app is in foreground?

Notification messages When your app is in the foreground, the FCM notification message is delivered to the onMessageReceived() handler and you can handle it by posting a notification if needed or update the app content with the FCM payload data (Max 4KB) or fetch content from app server.


1 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

like image 53
morepork Avatar answered Nov 02 '22 00:11

morepork