简体   繁体   English

FCM通知未在Oreo的后台调用

[英]FCM Notifications not getting called in background for Oreo

I am using FCM notifications for chat when user is not connected to xmpp. 当用户未连接到xmpp时,我正在使用FCM通知进行聊天。

There are 2 modes of Notifications in FCM 1. Notifications Message 2. Data Messages FCM中有两种通知模式。1.通知消息2.数据消息

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. 这种方法适用于除Oreo以外的所有版本。

For Oreo, I only get notifications if the app is not connected to xmpp and in foreground. 对于Oreo,如果应用程序未连接到xmpp且处于前台,我只会收到通知。 My onMessageReceived method is getting called. 我的onMessageReceived方法被调用。

But the same is not happening when the app is killed or removed from recents ONLY for Oreo. 但是,仅对于Oreo,当该应用程序被杀或从最新消息中删除时,情况不会发生。

Edit: I tried on One Plus 3 device. 编辑:我在One Plus 3设备上尝试过。

Any help is appreciated. 任何帮助表示赞赏。

This might be too late for an answer but One Plus phones have a feature called battery optimisation. 答案可能为时已晚,但One Plus手机具有一项称为电池优化的功能。 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. 我的One Plus 5T遇到了同样的问题。

From Oreo the concept of notification channels was introduced. 从奥利奥(Oreo)引入了通知渠道的概念。 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: 在清单中设置ID:

<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 notification_channel_id与变量CHANNEL_ID相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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