Android 不显示特定屏幕的推送通知
Android not show push notification for specific screen
我开发了一个接收 firebase 推送通知的应用程序。在该应用程序中,有一个在特定 ChatActivity
中运行的聊天部分。该服务如下所示:
class PushNotificationsService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
showPushNotification(remoteMessage)
}
}
我不想显示推送通知,以防我的 ChatActivity
可见。
处理这种情况的更常见或更好的正确方法是什么?
我尝试了以下选项,但不确定 100% 能否在生产环境中正常工作:
通过SharedPreferences
一直检查ChatActivity
生命周期
通过应用程序中定义的静态变量始终检查ChatActivity
生命周期Application.class
我认为选项 2) 可以很好地工作。您只需在应用程序 class 中有一个布尔值 isChatActivityVisible
变量,并根据 activity 的生命周期打开或关闭它。
您还可以查看此 link 以获得更多想法:Android: how do I check if activity is running?
稍后编辑:
我认为您也可以尝试使用广播并让广播接收器处理推送的不同方法。另一种方法是使用 Greenrobot 的 EventBus 并通过它处理推送。在这种情况下,您将在 onStart()/onStop()(或 onPause()/onResume())方法中注册和注销 EventBus,并将推送作为 pojo 发送。它会根据您的需要自动工作。
首先,您需要在 build.gradle(app) 文件中添加它的依赖项:
implementation 'org.greenrobot:eventbus:3.1.1'
你会有这样的东西(在 ChatActivity 中):
override fun onResume(){
super.onResume()
EventBus.getDefault().register(this)
}
override fun onPause(){
EventBus.getDefault().unregister(this)
super.onPause()
}
然后,无论您在哪里接收推送,您都将使用:
EventBus.getDefault().post(YourChatPushNotificationEvent(message))
在您的情况下,这将是:
class PushNotificationsService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
EventBus.getDefault().post(YourChatPushNotificationEvent(remoteMessage))
}
}
其中 YourChatPushNotificationEvent
包含您想要进入 ChatActivity 的任何有效负载(这是一个简单的 pojo,您可以在其中放置任何内容,在这种情况下,它有一个在构造函数中传递的字符串消息)。如果做得对,我看不出为什么这行不通。它可能会在您的应用程序中增加一些复杂性,因为它正在进一步开发,但我想如果您正确命名您的事件,您可以处理它。
要处理您刚刚发出的事件,您将在 ChatActivity 中包含:
@Subscribe(threadMode = ThreadMode.MAIN)
fun onYourChatPushNotificationEvent(event: YourChatPushNotificationEvent) {
//do whatever you need to do in here with the payload that is in the event object
}
祝你好运!
我开发了一个接收 firebase 推送通知的应用程序。在该应用程序中,有一个在特定 ChatActivity
中运行的聊天部分。该服务如下所示:
class PushNotificationsService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
showPushNotification(remoteMessage)
}
}
我不想显示推送通知,以防我的 ChatActivity
可见。
处理这种情况的更常见或更好的正确方法是什么?
我尝试了以下选项,但不确定 100% 能否在生产环境中正常工作:
通过
SharedPreferences
一直检查通过应用程序中定义的静态变量始终检查
ChatActivity
生命周期Application.class
ChatActivity
生命周期
我认为选项 2) 可以很好地工作。您只需在应用程序 class 中有一个布尔值 isChatActivityVisible
变量,并根据 activity 的生命周期打开或关闭它。
您还可以查看此 link 以获得更多想法:Android: how do I check if activity is running?
稍后编辑:
我认为您也可以尝试使用广播并让广播接收器处理推送的不同方法。另一种方法是使用 Greenrobot 的 EventBus 并通过它处理推送。在这种情况下,您将在 onStart()/onStop()(或 onPause()/onResume())方法中注册和注销 EventBus,并将推送作为 pojo 发送。它会根据您的需要自动工作。
首先,您需要在 build.gradle(app) 文件中添加它的依赖项:
implementation 'org.greenrobot:eventbus:3.1.1'
你会有这样的东西(在 ChatActivity 中):
override fun onResume(){
super.onResume()
EventBus.getDefault().register(this)
}
override fun onPause(){
EventBus.getDefault().unregister(this)
super.onPause()
}
然后,无论您在哪里接收推送,您都将使用:
EventBus.getDefault().post(YourChatPushNotificationEvent(message))
在您的情况下,这将是:
class PushNotificationsService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
EventBus.getDefault().post(YourChatPushNotificationEvent(remoteMessage))
}
}
其中 YourChatPushNotificationEvent
包含您想要进入 ChatActivity 的任何有效负载(这是一个简单的 pojo,您可以在其中放置任何内容,在这种情况下,它有一个在构造函数中传递的字符串消息)。如果做得对,我看不出为什么这行不通。它可能会在您的应用程序中增加一些复杂性,因为它正在进一步开发,但我想如果您正确命名您的事件,您可以处理它。
要处理您刚刚发出的事件,您将在 ChatActivity 中包含:
@Subscribe(threadMode = ThreadMode.MAIN)
fun onYourChatPushNotificationEvent(event: YourChatPushNotificationEvent) {
//do whatever you need to do in here with the payload that is in the event object
}
祝你好运!