Android Oreo 电池优化导致 FCM 延迟

Android Oreo battery optimization causes delays in FCM

我已经完全按照 if 的文档实现了 FCM:

我有这样的服务 public class TCMessagingService extends FirebaseMessagingService

我已经在清单中这样声明了它:

<service android:name=".services.TCMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
</service>

我使用 SDK 级别 25 进行定位和编译,我的 firebase 版本是 10.2.1。

现在的问题是,在 Android 8.0 上,有时我在收到推送通知时会有很大的延迟。它可能会在几分钟后出现。但这并不总是如此,有时事情就像预期的那样工作,推送通知来得非常快。

我尝试将 FCM 版本更新到最新版本,但这没有帮助。

但是当我在设置中关闭我的应用程序的电池优化时,一切正常。但这不是解决方案。 我该怎么做才能使 FCM 在 Android 8.0 上按预期工作?

当设备处于 doze mode 时,仅立即处理高优先级消息。因此,请确保您的 FCM 消息以优先级 "high".

发送

the documentation for Firebase messaging here, and the documentation for doze and FCM here

实际上我找到了解决方案。

为了修复 Android 8 个推送通知的延迟,您需要设置最小值 compileSdkVersion 25

classpath 'com.google.gms:google-services:3.1.0' 

在项目级别 gradle 文件中。

问题已解决,希望对大家有所帮助

你必须:

  • 更新编译版本为26
  • 将构建工具更新到 26.0.2
  • 最低 google 服务版本:11.0.4

  • 添加这个依赖:

    编译'com.firebase:firebase-jobdispatcher:0.5.2'

  • 将 firebase 插件更新为:

    类路径'com.google.gms:google-services:3.1.0'

  • 在您的项目中添加 google 存储库 gradle

    所有项目{ 存储库{ google() 中心() } }

  • 在您的 java 代码中,通知管理器是以另一种方式创建的 在 Android 中:

          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
             NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
             String id = "id_product";
             // The user-visible name of the channel.
             CharSequence name = "Product";
             // The user-visible description of the channel.
             String description = "Notifications regarding our products";
             int importance = NotificationManager.IMPORTANCE_MAX;
             NotificationChannel mChannel = new NotificationChannel(id, name, importance);
             // Configure the notification channel.
             mChannel.setDescription(description);
             mChannel.enableLights(true);
             // Sets the notification light color for notifications posted to this
             // channel, if the device supports this feature.
             mChannel.setLightColor(Color.RED);
             notificationManager.createNotificationChannel(mChannel);
          }
    

您可以在 link 中找到完整的代码。