FirebaseMessagingService 无法在后台使用一加 3 android oreo 8.0

FirebaseMessagingService is not working in background using one plus 3 android oreo 8.0

当应用不是 运行 并从最近的应用列表中删除时,Firebase onMessageReceived 从不调用。 我正在使用 Firebase 并从我的服务器向我的应用程序发送数据有效负载通知,我尝试使用 JobScheduler 每 1 分钟启动一次 MyFirebaseMessagingService 以防系统终止我的服务,但这种方法在 One Plus 上对我不起作用3. 我知道 android 对后台服务添加限制以优化电池使用,但此限制是否影响 FCM 服务?

这是我的消息字符串:

{
"notification": {
    "title": "Hello",
    "body": "Notification",
    "badge": 1,
    "icon": "app_icon",
    "color": "#ffffff",
    "sound": "default",
    "clickAction": "home",
    "tag": "parent_ccid_6"
},
"message": {
    "priority": "high",
    "type": "daily_tips",
    "data": {
        "parent_ccid": "parent_ccid_6",
        "id": "2",
        "sound": "default"
    }
},

}

这是我在清单中的服务

 <service
        android:name=".util.FCM.MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>

以上牛轧糖必须使用通知通道

样本

        int notifyID = 1;
        String CHANNEL_ID = "my_channel_01";
         // The id of the channel.
        CharSequence name = "channel ram";
         // The user-visible name of the channel.
        String groupId = "Building654651";
        CharSequence groupName = "BuilgingHub123";
        int importance = 0;
            importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
 Notification notification = new Notification.Builder(this)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(messageBody)
                .setSmallIcon(R.drawable.ic_notifi).setColor(Color.parseColor("#FFA23B"))
                .setChannelId(CHANNEL_ID)
                .build();
  NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.createNotificationChannelGroup(new NotificationChannelGroup(groupId, groupName));
        mNotificationManager.createNotificationChannel(mChannel);
            mNotificationManager.notify(notifyID, notification);

当应用程序在后台时,onMessageReceived 将永远不会根据 android 文档被调用。 Link:- https://firebase.google.com/docs/cloud-messaging/android/receive

当应用程序在后台运行时,通知会传送到设备的系统托盘,您的应用程序(即启动器屏幕)的默认意图将被调用,您必须在此屏幕上处理该通知。

其他解决方案是您可以从有效负载中删除 "notification" 关键字,然后当您的应用程序在后台时您将能够收到通知并且 onMessageReceived 将被调用。

我没有找到任何通过代码解决这个问题的方法。 恢复通知的唯一方法是转到设置和电池优化设置中的 select "Don't optimize",它在后台工作正常,否则不会触发通知处理程序。 我在一加 3 上安装的所有应用程序都遇到了这个问题。

尝试android:exported="正确"。在您的 android 清单文件中。