FCM Notifications not getting called in background for Oreo

北战南征 提交于 2019-12-19 11:37:12

问题


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



来源:https://stackoverflow.com/questions/49137842/fcm-notifications-not-getting-called-in-background-for-oreo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!