![](/img/trans.png)
[英]How can I get “data payload” from push notification (using FCM) when android app is in killed state?
[英]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.