繁体   English   中英

如何在应用程序被终止时处理 fcm 推送通知,而不使用 WakefulBroadcastReceiver,因为它在 26.1.0 中已弃用?

[英]How to hanlde fcm push notification when app is killed, without using WakefulBroadcastReceiver as its decprecated in 26.1.0?

当应用程序在后台或被终止时收到通知时,我想从我的应用程序调用某些服务。

早些时候,我使用WakefulBroadcastReceiver来处理此类通知,但现在已弃用。 有人可以帮忙吗?

提前致谢。干杯

使用 JobScheduler

从 Android O 开始,背景检查限制使此类不再普遍有用。 (从接收到广播开始服务通常是不安全的,因为此时您无法保证您的应用程序在前台,因此允许这样做。)相反,开发人员应该使用android。 app.job.JobScheduler来安排作业,这不需要应用程序在执行此操作时持有唤醒锁(系统将负责为作业持有唤醒锁)。

只需创建一个服务扩展FirebaseMessagingService并覆盖onMessageReceived

class MyFirebaseMessagingService : FirebaseMessagingService() {
  override fun onMessageReceived(remoteMessage: RemoteMessage?) {
    Log.d(TAG, "From: ${remoteMessage?.from}")
  }
}

将服务添加到您的清单

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

官方文件

您应该扩展常规BroadcastReceiver而不是WakefulBroadcastReceiver

然后,在onReceive您可以使用JobIntentService排队工作

暂无
暂无

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

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