【问题标题】:Android Oreo battery optimization causes delays in FCMAndroid Oreo 电池优化导致 FCM 延迟
【发布时间】:2017-10-26 17:28:49
【问题描述】:

我已经实现了 FCM,就像 if 文档中所说的那样:

我有这样的服务 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 上按预期工作?

【问题讨论】:

  • 你是否在打盹模式或默认模式下测试过它?

标签: android firebase-cloud-messaging android-8.0-oreo


【解决方案1】:

当设备位于doze mode 时,只会立即处理高优先级消息。因此,请确保您的 FCM 消息以“高”优先级发送。

请参阅 the documentation for Firebase messaging heredocumentation for doze and FCM here

【讨论】:

  • 默认情况下,优先级为 HIGH,但在 Android Oreo 上对我不起作用
【解决方案2】:

其实我找到了解决办法。

为了解决 Android 8 推送通知的延迟问题,您需要设置最低 compileSdkVersion 25

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

在项目级 gradle 文件中。

这解决了问题,希望它可以帮助某人

【讨论】:

  • @Fakher 你确定 SDK 的编译版本是 25 吗?和 buildToolsVersion 和 targetSdkVersion 一切都指向 25 ?
【解决方案3】:

你必须:

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

  • 添加这个依赖:

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

  • 将 firebase 插件更新为:

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

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

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

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

          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找到完整的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    • 2018-06-01
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多