3

I'm facing a very weird problem of not receiving payload data second time when the app is in background.

For ex - (When I had not started my app yet)

  • When I receives notification for the first time it will open my app and read the extras from the notification.
  • When I receive the second notification it maximises my app or bring my app to the front but does not read the extras.

When my app is open everything works fine. Even when I put my app on background after manually opening it then also all notifications work fine.

Here is my manifest code

      <activity
            android:name=".PagerActivity"
            android:launchMode="standard">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

Here is my activity code

 @Override
    public void onNewIntent(Intent newIntent) {
        this.setIntent(newIntent);

        // Now getIntent() returns the updated Intent
        if(getIntent().hasExtra("sno")){
            Log.d("New Intent DATA", "Yes it has the data sno is: " + getIntent().getExtras().getString("sno"));
        }


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getIntent().hasExtra("sno")){
            Log.d("DATA", "Yes it has the data");
        }
    }

I tried researching a lot but to no avail. Please let me know if you need any other details.

I'm using firebase to send notification with custom data. I also tried it sending via web server but still didn't got it working.

I debugged my app and found that when I'm opening my app through second notification new instance is not being created.

Jack Stern
  • 395
  • 1
  • 4
  • 16
  • Possible duplicate of [Open app on firebase notification received (FCM)](http://stackoverflow.com/questions/37554274/open-app-on-firebase-notification-received-fcm) – mallaudin Nov 17 '16 at 10:00
  • Have you implemented `onMessageReceived` ? – mallaudin Nov 17 '16 at 10:05
  • @mallaudin OnMessageReceived is for when your app is in foreground I guess. Here my app is in background. – Jack Stern Nov 17 '16 at 10:20
  • `onMessageReceived` will be called for `data messages` even if the app is in background. – mallaudin Nov 17 '16 at 10:23
  • I'm sending notification and data both. If your app is in the background or closed then a notification message is shown in the notification center, and any data from that message is passed to the intent that is launched as a result of the user tapping on the notification. – Jack Stern Nov 17 '16 at 10:25
  • `There are two types of messages data messages and notification messages. Data messages are handled here in onMessageReceived whether the app is in the foreground or background.` This is the comment in sample app. https://github.com/firebase/quickstart-android/blob/master/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/MyFirebaseMessagingService.java – mallaudin Nov 17 '16 at 10:26
  • Sorry but the OnMessageReceived is not triggered when then app is in background or close. – Jack Stern Nov 17 '16 at 10:33
  • have you registered service in manifest? – mallaudin Nov 17 '16 at 10:35
  • yes, I have registered – Jack Stern Nov 17 '16 at 10:38
  • I debugged my app and found that when I'm opening my app through second notification new instance is not being created. – Jack Stern Nov 17 '16 at 10:39
  • Can you put your log in onResume and check what happens? – mallaudin Nov 17 '16 at 10:47
  • I checked it shows the data from the 1st notification only even after receiving second notification with different data. – Jack Stern Nov 17 '16 at 10:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128338/discussion-between-mallaudin-and-jack-stern). – mallaudin Nov 17 '16 at 11:03

2 Answers2

2

When you send both notification and data payload and if your app is in background, notification will be shown in the tray, but on clicking the notification if your launching activity is in background, it will be brought to foreground and you will not receive data payload.

One solution is to set the launch mode of that specific actvitiy to singleTask and implement onNewIntent in the activity to receive data payload.

In this way, your activity will be brought to foreground and activities residing on the top of this activity in the stack will be destroyed.

You can find more about launch modes here.

mallaudin
  • 4,744
  • 3
  • 36
  • 68
1

There are two different type of message

  1. Notification Message
  2. Data Message

Payload for notification message

 {
    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification" : {
      "body" : "great match!",
      "title" : "Portugal vs. Denmark",
      "icon" : "myicon"
    }
  }

Payload for data message

{
   "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
   "data" : {
     "Nick" : "Mario",
     "body" : "great match!",
     "Room" : "PortugalVSDenmark"
   },
 }

Messages with both notification and data payloads

App behavior when receiving messages that include both notification and data payloads depends on whether the app is in the background or the foreground—essentially, whether or not it is active at the time of receipt.

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.

if you want to pass the data along with the notification to work on both state of application(active and inactive) you have to use data message.

Further details is availble on the official documentation About FCM Messages(Message Types)

rana_sadam
  • 1,216
  • 10
  • 18