Android Firebase Push Notification With Custom Click Action
101 6 19189
If the user click a firebase notification by default it will open the launcher activity of that application. If you want to open a specific activity when user click the notification, in that case you need to provide an additional attribute called click_action with your firebase notification.
Like my Facebook page : https://www.facebook.com/codeglympse
Subscribe My YouTube channel : http://www.youtube.com/ticoontechnologies
By anonymous 2017-09-20
For notifications to be clickable when your app is in the background, you need the click_action
attribute in your notification payload.
Please check this section of the Firebase docs.
Also, when you define the click_action
attribute, you will also need a corresponding <action>
attribute in the <intent-filter>
of the activity that you wish to launch.
This video explains it in quite a detailed manner.
Though, please note that you can not set the click__action
attribute if you're sending notifications from the Firebase Console. You can only do so if you send a notification from your own Admin server or using Firebase Cloud Functions.
Lastly, in the activity that is launched, you can set additional Data
using the data attribute ( also shown in the same doc that I linked above ). And when you launch your app by clicking on a notification, you can obtain the notification data using getIntent()
. Check out this answer for more details on how to do that.
For example, if your notification payload has the following structure,
payload = {
notification: {
title: `You ordered a new product`,
click_action : 'HANDLE_NOTIFICATION',
},
data : {
product_id : 'ABC98292',
type : `Clothes`,
product_name : 'Cotton spring shirt'
}
};
then, put the filter in the tag of the activity that you want to open when the notification is clicked. An example is the following :-
<intent-filter>
<action android:name="HANDLE_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
Then you can get the product_id from the notification using getIntent().getStringsExtra("product_id")
and so on.
This way, you'll be opening the required activity, and can populate it with the relevant details obtained from your notification.
By anonymous 2017-09-20
For notifications to be clickable when your app is in the background, you need the click_action
attribute in your notification payload. This is completely separate from the FirebaseMessagingService
and other such classes, because these work when the app is in the foreground.
Please check this section of the Firebase docs.
Also, when you define the click_action
attribute, you will also need a corresponding <action>
attribute in the <intent-filter>
of the activity that you wish to launch.
This video explains it in quite a detailed manner.
Though, please note that you can not set the click__action
attribute if you're sending notifications from the Firebase Console. You can only do so if you send a notification from your own Admin server or using Firebase Cloud Functions.
Lastly, in the activity that is launched, you can set additional Data
using the data attribute ( also shown in the same doc that I linked above ). And when you launch your app by clicking on a notification, you can obtain the notification data using getIntent()
. Check out this answer for more details on how to do that.
It really is quite simple and elegant.
UPDATE
For example, if your notification payload has the following structure,
payload = {
notification: {
title: `You ordered a new product`,
click_action : 'HANDLE_NOTIFICATION',
},
data : {
product_id : 'ABC98292',
type : `Clothes`,
product_name : 'Cotton spring shirt'
}
};
then, put the filter in the tag of the activity that you want to open when the notification is clicked. An example is the following :-
<intent-filter>
<action android:name="HANDLE_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
Then you can get the product_id from the notification using getIntent().getStringsExtra("product_id")
and so on.
This way, you'll be opening the required activity, and can populate it with the relevant details obtained from your notification.
By anonymous 2018-03-05
I had a similar problem couple weeks ago;) You see, if you are in background, the data payload is delivered in the extras of the intent of your launcher Activity. You can define both action and intent filter in manifest xml and notification json as here
Firebase (FCM): open activity and pass data on notification click. android
and just get the data in coresponding activity.
If you have to manage special logic maybe better examples are like here:
open a specific activity from firebase notification
and here:
Submit Your Video
By anonymous 2017-09-20
For notifications to be clickable when your app is in the background, you need the
click_action
attribute in your notification payload.Please check this section of the Firebase docs.
Also, when you define the
click_action
attribute, you will also need a corresponding<action>
attribute in the<intent-filter>
of the activity that you wish to launch.This video explains it in quite a detailed manner.
Though, please note that you can not set the
click__action
attribute if you're sending notifications from the Firebase Console. You can only do so if you send a notification from your own Admin server or using Firebase Cloud Functions.Lastly, in the activity that is launched, you can set additional
Data
using the data attribute ( also shown in the same doc that I linked above ). And when you launch your app by clicking on a notification, you can obtain the notification data usinggetIntent()
. Check out this answer for more details on how to do that.Original Thread