Determine if an event has been opened from FCM usage notification.

I use the fcm console to send messages to all devices on which my application is installed, in notifications there is no additional payload for notification only.

I want to know if there is an easy way to find out if an event has been opened from the FCM notification notification.

There is a solution for this by expanding the FirebaseMessagingService service and creating a notification yourself.

I want to know if there is another solution without extending the service or sending additional notifications.

Are there any additional ones that are transmitted using the intent that is used to open the operation from the notification?

+5
source share
3 answers

I hope you know that FCM-based messaging created two types of notifications

First, the notification that we create with onMessageReceived() method, it should be noted that onMessageReceived() will fire if the application is in the foreground.

Secondly, FCM creates its own default notification when the application is in the background, in which case onMessageReceived() not intercepted, and therefore we cannot set the pending intent on our own.

Note: the above types become effective when you send a push notification message to your application in case the message me "data" onMessageReceived() triggered regardless of whether the application is in the foreground or background (notifications are sent). from the FCM console there are "notifications" such as push messages)

Returning to your question, it is unclear whether you are sending a push message from the FCM console or making a request to the FCM server, so let's take a look here.

  1. The message is sent by the FCM console:
    send the data from the advances section in the notification panel to FCM, which looks like this

enter image description here

when the application is in the foreground onMessageReceived() will be intercepted, use the code below to get the data payload

 public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = "MyFirebaseMsgService"; @Override public void onMessageReceived(RemoteMessage remoteMessage) { //Displaying data in log //It is optional //Calling method to generate notification sendNotification(remoteMessage.getData()); } //This method is only generating push notification //It is same as we did in earlier posts private void sendNotification(Map<String, String> messageBody) { Intent intent = new Intent(this, SplashScreen.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity( this, 0, setNotificationRoute(messageBody), PendingIntent.FLAG_UPDATE_CURRENT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(android.R.drawable.sym_def_app_icon) .setContentTitle(messageBody.get("title")) .setContentText(messageBody.get("text")) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notificationBuilder.build()); } private Intent setNotificationRoute(Map<String, String> messageBody) { Intent intent = null; String type = messageBody.get("type"); if (type != null) { switch (type) { case "android": //intercept your payload here to create swit intent = new Intent(this, MainActivity.class); break; default: break; } } return intent; } } 

and if the application is in the background, then when the notification is clicked, the application will be open for the "default" action, you can set any action as the default action by adding the following lines to the application manifest in the action intent filter:

 <category android:name="android.intent.category.DEFAULT" /> 

In this exercise, you can call to get deliberate additions and then get a payload of data to decide which exercise you need to land on. Code below

  . . . Bundle bundle = getIntent().getExtras(); if (bundle != null) { setNotificationRoute(getIntent().getExtras()); . . } private void setNotificationRoute(Bundle extras) { String type = extras.getString("type"); Intent intent = null; if (type != null) { switch (type) { case "message": String room = extras.getString("room"); intent = new Intent(this, MainActivity.class); startActivity(intent); break; default: break; } } } 
  1. Message sent from the FCM server: a message sent from the aforementioned FCM console is similar to sending the json body below as a post request to the FCM server:

     { "notification": { "title": "Hi Tester", "text": "News for testing", "sound": "default", "badge": 0 }, "data":{ "type": "credits" }, "priority": "high", "to": "{{device_token}}" } 

the notification interception process will be the same for this case.

+8
source

if you want to start any action without extending the FireBaseMessaging class, you can set these flags using your FCM data payload.

Therefore, whenever a user clicks on the FCM notification in the system tray. A certain action must open. And if you want to get additional package data, you can get it with getIntent (). getExtras (); Method.

  "message":{ "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", "notification":{ "title":"Match update", "body":"Arsenal goal in added time, score is now 3-0" }, "android":{ "ttl":"86400s", "notification"{ "click_action":"MainActivity.class" // define your activity here } }, "payload": { "aps": { "category": "NEW_MESSAGE_CATEGORY" } } } 

See link below: -

"sets the appropriate keys to determine the result of a user clicking a notification about Android and iOS - click_action and category, respectively."

0
source

No need to transfer the extra with the intention. Just set the flags as

  Intent lIntent = new Intent(getApplicationContext(),YourActivity.class); lIntent.addFlags(Intent.FLAG_ACTIVI`enter code here`TY_CLEAR_TOP); lIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); lIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(lIntent); 
-one
source

Source: https://habr.com/ru/post/1012523/


All Articles