简体   繁体   中英

FCM Notifications not getting called in background for Oreo

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM