首页 > 解决方案 > 如果应用程序在前台,如何停止 Firebase 通知

问题描述

Is there any way to stop showing the push notification when the app is in foreground ? 

现在我们正在接收应用程序处于前台或后台的所有消息,但我只想在应用程序处于前台或最小化时显示 Firebase 推送通知。

标签: androidiosflutter

解决方案


显示通知取决于我们。我们可以配置只在应用程序处于后台时显示通知,并在应用程序处于前台时禁用。我们可以检查应用程序是在前台还是在后台,如下所示。

private boolean isAppInForeground(Context context) {

    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> runningProcesses = activityManager.getRunningAppProcesses();
    if (runningProcesses == null) {
        return false;
    }
   String packageName = context.getPackageName();
   for (RunningAppProcessInfo runningProcess : runningProcesses) {
   if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && runningProcess.processName.equals(packageName)) {
       return true;
    }
}
return false;

}

如果它返回 true,则不显示通知。


推荐阅读