简体   繁体   English

有没有办法限制特定屏幕的 fcm 通知?

[英]Is there any way to limit fcm notification for a specific screen?

I have a question about the FCM notification limit.我对 FCM 通知限制有疑问。 Is there any way that I can limit FCM notifications for a specific screen?有什么方法可以限制特定屏幕的 FCM 通知? For example, if there are three screens, screen A, screen B, and screen C, the notification should be received on-screen A and B but should not be received while user is viewing screen C.例如,如果存在三个屏幕,屏幕 A、屏幕 B 和屏幕 C,则应在屏幕 A 和屏幕 B 上接收通知,但不应在用户查看屏幕 C 时收到通知。

If I am right, I think it has to check if screen C is showing on the device before sending notifications.如果我是对的,我认为它必须在发送通知之前检查屏幕 C 是否显示在设备上。 So how can I check which screen is showing and how to limit notifications for a specific screen?那么如何检查正在显示的屏幕以及如何限制特定屏幕的通知?

One way to do it would be to intercept all fcm if app is in foreground, to check if app is in foreground you can use this in your FCMService一种方法是在应用程序在前台时拦截所有 fcm,以检查应用程序是否在前台,您可以在FCMService中使用它

if(ProcessLifecycleOwner.get().lifecycle == Lifecycle.State.RESUMED)
   LocalBroadcastManager.sendBroadcast(Intent())

you need to add lifecycle dependencies for it您需要为其添加生命周期依赖项

    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
    kapt "android.arch.lifecycle:compiler:1.1.1"

then you can register a broadcast receiver from your screen C and send explicit broadcast from FCMService.然后您可以从屏幕 C 注册广播接收器并从 FCMService 发送显式广播。

Note that the broadcast receiver is registered on screen C onResume and unregistered on onDestroy .请注意,广播接收器在屏幕 C onResume上注册并在onDestroy上取消注册。

More info on how to use explicit BroadcastReceiver here有关如何在此处使用显式 BroadcastReceiver 的更多信息

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

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