Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google FCM getIntent not returning expected data when app is in background state

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:

  • https://github.com/firebase/quickstart-android/issues/4
  • https://github.com/firebase/quickstart-android/issues/47
  • How to handle notification when app in background in Firebase
  • Firebase onMessageReceived not called when app in background
  • How to handle notification when app in background in Firebase

Thanks in advance. Sorry for bad English.

like image 214
Md. Sajedul Karim Avatar asked Aug 19 '16 19:08

Md. Sajedul Karim


1 Answers

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.

like image 96
Md. Sajedul Karim Avatar answered Nov 15 '22 16:11

Md. Sajedul Karim