3

I am using Firebase Cloud Messaging on Android and I ran across the following issue. It occurs when handling notifications received while the application is in background. The documentation states the following:

When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.

This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

Even though this is what happens most of the times, I have found a few edge cases when the launcher activity is not opened. Instead, the application is simply resumed from background on the last activity in the backstack, leaving me with no way of obtaining the payload.

I can reproduce this issue every time by killing the process and having the application opened via a notification from the system tray. The first time the notification is tapped, the launcher activity gets opened as described in the docs, but by tapping on any subsequent notification, the application is simply resumed without going through the launcher activity first.

I am not sure if I am doing something wrong or if there is a bug in Firebase's SDK. Do you have any idea why this might be happening?

The notification I am testing with is a display-message notification (not data notifaction) and it also has a payload.

{
    "to": "d-3qyvyqefU:APA91bG_nHNYeYuKwB3oIvRKStVgSyelTIhX6rtu6LGPAjgo-LGDVk9iFO5LWZ-XFMeeRVkZm0suMyJAnWORWbZlMaVcHmhFTZOKPI_A-D2wYXM0SIIT8pZZ2g4W55S1bz9YD5y625fv",
    "priority": "high",
    "content_available": true,
    "registration_id": "d-3qyvyqefU:APA91bG_nHNYeYuKwB3oIvRKStVgSyelTIhX6rtu6LGPAjgo-LGDVk9iFO5LWZ-XFMeeRVkZm0suMyJAnWORWbZlMaVcHmhFTZOKPI_A-D2wYXM0SIIT8pZZ2g4W55S1bz9YD5y625fv",
    "collapse_key": "Collapse Key",
    "data": {
        "type": "default",
        "person": "2123435",
        "token": "asdmmio23j4123azde3"
    },
    "notification": {
        "type": "default",
        "title": "Howdie!",
        "body": "Say hi to your new friend!"
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Alexandru Pele
  • 1,123
  • 7
  • 12
  • you can open your launcher activity when click in notification by give flags in intent like CLEAR_TOP && CLEAR_TASK && NEW – Vishal Patoliya ツ Aug 31 '16 at 08:50
  • @VishalPatoliya but the problem is that when the application is in background it's the FCM SDK that is creating the notification for me. I have no way of telling it what flags to use for the intent – Alexandru Pele Aug 31 '16 at 08:52
  • you want to like that when you tap on notification and app is in background it should be start from launcher activity not from your last activity right ? – Vishal Patoliya ツ Aug 31 '16 at 08:54
  • @VishalPatoliya yes, exactly, but this is the "default" behaviour that is described in the documentation. It should already happen this way, and it actually does almost every time, except for the case that I described in the question. I am not sure why though... – Alexandru Pele Aug 31 '16 at 08:56
  • @AlexandruPele I am also facing this issue have you find any solution on that? – Hardik Mehta Feb 20 '17 at 10:33
  • @HardikMehta yes. check the answer! I hope it'll help. – Alexandru Pele Feb 22 '17 at 13:25

1 Answers1

0

To all those that are still facing this issue, there is a method in the FirebaseMessagingService class that exposes an Intent object. You can override it and do something like this:

@Override
protected int zzaa(Intent intent) {
   intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
   return super.zzaa(intent);
}

The code above should fix the issue, but most likely the guys at Firebase will change the access modifier in the future - if they haven't already. I've opened a ticket on their end and got confirmation that they can reproduce this behaviour and will fix the bug at some point.

Alexandru Pele
  • 1,123
  • 7
  • 12