Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Firebase MyFirebaseMessagingService not called when app is swiped from recent list

Edit 24/11/2016: I've just received word from Firebase Support. They suggest this is an Asus issue. The device I'm using is an Asus ZenFone 2 Laser. Here's their response:

The issue here is that Google does not control how manufacturers customize their version of android. If Asus is following the Android guidelines, then the notification should work (like on the other devices).

One thing that we found is that some phones put the apps in force-stop state, which prevents the app from receiving the notification.

From your test result this doesn't seems to be the case. BUT there are many other ways in which Asus can stop notifications from working, and unfortunately we cannot check all of them.

It would be best for you to contact Asus support and let us know if there are any news.

As this is a low priority issue for my client, I'm not going to pursue it further at the moment. I unfortunately don't have the time to test the same issue on other devices. If this changes in the future and I'll update this issue accirdingly. Until now, I will consider this unsolved. I would be interested to hear if others are having the same issues on other brand phones.

As for Asus users, there's one thing to consider: There are power saving settings that disable notificaitons.

Edit 30/09/2016: I've contacted Firebase support about this issue. They seem to agree with what Arthur Thompson is saying:

  • Stopped apps should not receive messages.
  • Swiping an app from the recent list should not stop an app.

The interesting part is that my app doesn't seem to be stopped. When running adb shell dumpsys package | grep stopped my package returns:

User 0:  installed=true hidden=false stopped=false notLaunched=false enabled=0

This indicates it's not stopped and should still receive messages (according to the very helpful Firebase Support engineer). However the app is not receiving the messages.

Edit 27/09/2016: I've omitted the notification object in the payload and moved that information do the data object instead. This doesn't solve the issue. I think I haven't been clear enough on what the issue is. The issue is that when the app is killed (swiped out of recent) the service doesn't get called or receive notifications anymore.

Original post

There's similar posts out there, but none that address this issue or give a solution.

I'm implementing FCM to get notifications. (I'm fairly new at FCM.) The message payload is a combo of both notification and data. When the app is in the foreground the service fires the onMessageReceived(). When the app is in the background (i.e. I push the home button), onMessageReceived() doesn't fire (as expected) but simply shows a notification. I can still see the log statements on MyFirebaseMessagingService.onCreate() and onDestroy(), which is great.

Now when I close the app by pushing the 'recent apps' button, and swiping the app out of the list, I no longer receive any notifications. onCreate() and onDestroy() are no longer called. No notification appears. I would like a notification to appear in this scenario too.

Example payload:

{
    "to":"token",
    "priority":"high",
    "notification": {
        "body":"hello",
        "title":"Message from Mom",
        "click_action":"ACTIVITY_MAIN"
    },
    "data":{
        "open":"chat"
    }
}

FCM Service:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onCreate() {
        super.onCreate();
        Log.v(DmcApplication.TAG, "Service Created");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.v(DmcApplication.TAG, "Service Destroyed...");
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.v(DmcApplication.TAG, "Message received!");
        EventBus.getDefault().post(new MessageEvent(remoteMessage.getNotification().getBody()));
    }

}

Bit of the manifest (in case relevant)

<service
    android:name="com.example.package.myFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

Logs:

//Log when app is in foreground and message comes in:
V/coo: Service Created
V/coo: Message received!
V/coo: Service Destroyed...

//Log when app is in background and message comes in:
V/coo: Service Created
V/coo: Service Destroyed...

//Log when app is killed:
<silence...>

Any help would be greatly appreciated as I've been stuck with this for days now.

like image 514
Coo Avatar asked Oct 30 '22 19:10

Coo


1 Answers

I ran into the same issue, looking at your "priority:high" string it looks like you are sending to Android and iOS. I ended up getting rid of the notification portion of the message and just putting the information that I needed into the data message. The data payload part triggers your service even when it is backgrounded or removed from recent apps.

On my server I check to see if the user registered their token from an iOS device or an Android device and send out the payload accordingly with the notification payload for iOS devices and the data payload for Android devices.

like image 158
Jeremiah Avatar answered Nov 11 '22 11:11

Jeremiah