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.
- 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
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; } } }
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.