I am implementing FCM (Firebase messaging Service) in my application. Here all seems ok except when app is in background state i am not able to extract expected notification data.
Based on concepts: There are two types of messages in FCM:
display-messages: These messages only work when your app is in foreground.
data-messages: Theses messages work even if your app is in background When our app is in the background, Android directs notification messages to the system tray.
for handling data-messages your notification should have click_action = "YOUR_ACTION" field.
My message will like this:
{
"data": {
"body": "here is body",
"title": "Title",
"click_action": "YOUR_ACTION"
},
"to": "ffEseX6vwcM:APA91bF8m7wOF MY FCM ID 07j1aPUb"
}
The Activity will display the message that manifest file will like this:
<activity
android:name=".NotificationActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Dialog"
android:windowSoftInputMode="stateHidden" >
<intent-filter>
<action android:name="YOUR_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
After clicking on Notification it will redirect to my NotificationActivity
. In my NotificationActivity
in onCreate
and onNewIntent
method i am extracting message using this way:
Bundle bundle=getIntent().getExtras();
if(bundle!=null) {
for (String key : bundle.keySet()) {
Object value = bundle.get(key);
Log.d("DATA_SENT", String.format("%s %s (%s)", key,
value.toString(), value.getClass().getName()));
}
}
Unfortunately in my NotificationActivity
i am getting below message:
google.sent_time: 1471631793774
from: 50711789666
google.message_id 0:1471631793776823%098e508d098e508d
collapse_key: com.myapp.package_name
But where is my expected notification data?
Here is my system configuration:
Android Studio Version: 2.1.3
Firebase Version: com.google.firebase:firebase-auth:9.0.1
Google Play service version : com.google.android.gms:play-services:9.2.1
Here are some related links:
Thanks in advance. Sorry for bad English.
The upper side android solution is correct. Actually, the notification message was the the problem. It sending me "data" object but not "notification" object. Lacks of "notification" object in my TargetActivity
didn't getting message using getIntent()
. After sending "notification" object it resolved my problem.
Correct message format is given bellow:
{
"data": {
"body": "here is body",
"title": "Title"
},
"notification": {
"body": "here is body",
"title": "Title",
"click_action": "YOUR_ACTION"
},
"to": "ffEseX6vwcM:APA91bF8m7wOF MY FCM ID 07j1aPUb"
}
Here is more clear concepts about firebase message. I found it from their support team. Firebase has three message types:
Notification messages : Notification message works on background or foreground. When app is in background, Notification messages are delivered to the system tray. If the app is in the foreground, messages are handled by onMessageReceived() or didReceiveRemoteNotification callbacks. These are essentially what is referred to as Display messages.
Data messages: On Android platform, data message can work on background and foreground. The data message will be handled by onMessageReceived(). A platform specific note here would be: On Android, the data payload can be retrieved in the Intent used to launch your activity. To elaborate, if you have "click_action":"launch_Activity_1", you can retrieve this intent through getIntent() from only Activity_1.
Messages with both notification and data payloads: When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification. When in the foreground, your app receives a message object with both payloads available. Secondly, the click_action parameter is often used in notification payload and not in data payload. If used inside data payload, this parameter would be treated as custom key-value pair and therefore you would need to implement custom logic for it to work as intended.
Also, I recommend you to use onMessageReceived
method (see Data message) to extract the data bundle. From your logic, I checked the bundle object and haven't found expected data content. Here is a reference to a similar case which might provide more clarity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With