I am using the fcm console to send messages to all of devices that have my app installed, the notifications don't have any extra payload only the notification message.
I want to know if there is a simple way to know if an Activity was opened from an FCM notification click.
There is a solution for that by extending the FirebaseMessagingService
service and creating a notification by yourself.
I want to know if there is another solution without extending the service or passing extras to the notification.
Is there any extra that is being passed with the Intent that is used for opening the activity from the notification click?
if you want to Start any Activity without extending FireBaseMessaging class then you can set this flags with your FCM data payload.
So whenever the user clicked on FCM Notification on System Tray.That defined Activity should open.And if you want to get extra bundle data.you can get it using getIntent().getExtras(); method.
Please refer to below link:-
"sets the appropriate keys to define the result of a user tap on the notification on Android and iOS — click_action, and category, respectively."
No need pass any extra with intent. Just set the flags like
I hope you know that FCM based messaging created two types of notification
Firstly, the notification that we create from on
onMessageReceived()
method, the point to note here is thatonMessageReceived()
will be triggered if the app is in foreground.Secondly, FCM creates its own default notification when app is in background, in this case
onMessageReceived()
is not intercepted and hence we cannot set a pending intent on our own.Note : the above mentioned types comes to play when you are sending a "notification" push message to your app in the case of a me "data" message
onMessageReceived()
is triggered irrespective of the app being in foreground or background (the notifications sent from FCM console are "notifications" type push messages)Coming back to your question, it is not clear if you're sending a push message from FCM console or making a FCM server request, so let's make cases here.
send data payload from the advance section in notification panel on FCM which looks like this
when app is in foreground
onMessageReceived()
will be intercepted use the code below to receive the data payloadand if app is in background, then on notification click app will be opened on a "default" activity, you can set any activity as a default one by adding the following lines in the app manifest in the activity's intent filter:
inside this activity you can call to get intent extras and then get the data payload to decide on which activity you need to land on. The code is below
Message sent from FCM Server: the message sent from FCM consolse above is same as sending the below json body as post request to FCM server:
the process of intercepting the notifications will be same for this case.