简体   繁体   English

在Android中-当应用程序处于前台时如何防止Firebase推送通知?

[英]In android - How to prevent Firebase push notification when app is in foreground?

I have made a push notification service in firebase and sending data notifications and receiving it in onMessageReceived(RemoteMessage remoteMessage) very well, 我在onMessageReceived(RemoteMessage remoteMessage)做了一个推送通知服务,并在onMessageReceived(RemoteMessage remoteMessage)很好地发送了数据通知并接收了它,

I am well aware when to send which type of notifications and that's why I have used data notifications to get it when the app is killed. 我很清楚何时发送哪种类型的通知,这就是为什么我在应用被终止时使用数据通知来获取它的原因。

But in my case, I do not want to send any notification when app is in Foreground mode, as onMessageReceived(RemoteMessage remoteMessage) will always get called when the app is in Foreground mode. 但就我而言, 我不想在应用程序处于前景模式时发送任何通知,因为当应用程序处于前景模式时, onMessageReceived(RemoteMessage remoteMessage)将始终被调用。

How to prevent this situation? 如何预防这种情况?

My notification format is this.. 我的通知格式是这样。

{
   "to" : "cGPhwcLbe5Y:APA91bFVAR6n9GxbDBovzd8X9nMBvKZ4Z098FzmkMGGUAuMhdps4uEof8mppUh7vrFTAsdFGzyurHa4gQ1nWgP-ncaN53ZtHziLi8YdHMEOGvtx6pA7ttCY5QhE5XKdFZ2QfX",
   "data" : {
    "message":"hi"
 }

}

There is 2 different type of message you can send from your server. 您可以从服务器发送两种不同类型的消息。

在此处输入图片说明

Where Notification message is handle by firebase itself. 其中, 通知消息Firebase本身处理。

Data message is custom payload, where you need to manage notification from your self. 数据消息是自定义有效负载,您需要在其中管理自己的通知。

so you need to use data key in your server script. 因此您需要在服务器脚本中使用数据密钥。

and just check that which app is running in top stack from this answer https://stackoverflow.com/a/27689352/942224 然后只需从此答案中检查哪个应用程序正在顶层堆栈中运行:https://stackoverflow.com/a/27689352/942224

private boolean isAppInForground(Context context, String packageName){
  String[] activePackages;
  ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
   activePackages = getActivePackages(am);

   if (activePackages != null) {
       return activePackages[0].equals(packagName)
   }
    return false;
}



private String[] getActivePackages(ActivityManager mActivityManager) {
final Set<String> activePackages = new HashSet<>();
final List<ActivityManager.RunningAppProcessInfo> processInfos = mActivityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
            if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                activePackages.addAll(Arrays.asList(processInfo.pkgList));
            }
        }
        return activePackages.toArray(new String[activePackages.size()]);
    }

Notifications are delivered to your onMessageReceived callback only when your app is in the foreground. 仅当您的应用程序位于前台时,通知才会传递到onMessageReceived回调。 If your app is in the background or closed then a notification message is shown in the notification center. 如果您的应用程序处于后台或关闭状态,则通知中心会显示一条通知消息。 If you want to prevent that onMessageReceived is being called when your app is in the foreground. 如果要防止在应用程序处于前台时调用onMessageReceived just get rid of FirebaseMessagingService from your app. 只需从您的应用中删除FirebaseMessagingService

Check the docuemnt: Send your First Message to a Backgrounded App 检查文档: 将您的第一条消息发送到后台应用程序

暂无
暂无

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

相关问题 应用程序在前台时如何处理Firebase通知 - How to handle the Firebase notification when app is in foreground 应用程序在前台时推送通知(使用 Firebase 触发器) - Push notification when app is in foreground (using Firebase triggers) 前台应用程序抖动时未收到 Firebase 推送通知? - Firebase Push Notification not received when flutter app in foreground? 当应用程序在 Android 上处于前台时不显示 Amazon Pinpoint 推送通知 - Amazon Pinpoint Push Notification not displayed when the app is in foreground on Android Android Firebase:当应用程序处于前台时如何从Firebase获取通知消息的消息文本组件 - Android Firebase : how get the message text component of notification message from firebase when app is in foreground 当应用程序处于前台时发送推送通知 - Push notification is sent when app is in foreground Android:Firebase 应用程序在前台时推送通知不同的图标,而当应用程序在后台时,另一个图标 - Android: Firebase push notification different icon while app is on foreground and another while app is on background Android 在 Firebase 通知上将应用程序带到前台 - Android bring app to foreground on Firebase notification 当应用程序在前台使用 Firebase 时不显示通知 - Notification not display when app in foreground using Firebase Android 仅当应用程序处于前台时才播放推送通知声音,但当应用程序处于后台时不播放声音 - Android Push notification sound is played only when app is in foreground but not playing sound when app is in background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM