FCM Notifications not getting called in background

2020-03-30 06:37发布

问题:

I am using FCM notifications for chat when user is not connected to xmpp.

There are 2 modes of Notifications in FCM 1. Notifications Message 2. Data Messages

I an using Data messages as Notification messages won't come if my app is cleared from recents

This approach is working fine for all the version except Oreo.

For Oreo, I only get notifications if the app is not connected to xmpp and in foreground. My onMessageReceived method is getting called.

But the same is not happening when the app is killed or removed from recents ONLY for Oreo.

Edit: I tried on One Plus 3 device.

Any help is appreciated.

回答1:

This might be too late for an answer but One Plus phones have a feature called battery optimisation. You will start receiving the background notifications as soon as you turn the battery optimisation off for your app. I had the same problem with my One Plus 5T.



回答2:

From Oreo the concept of notification channels was introduced. Check here.

Create a channel on start up:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    // Create the NotificationChannel
    CharSequence name = getString(R.string.channel_name);
    String description = getString(R.string.channel_description);
    int importance = NotificationManager.IMPORTANCE_DEFAULT;
    NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
    mChannel.setDescription(description);
    // Register the channel with the system; you can't change the importance
    // or other notification behaviors after this
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(mChannel);
}

Set the ID in your manifest:

<meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="@string/notification_channel_id"/>

notification_channel_id is the same as the variable CHANNEL_ID