android - 如果应用程序在前台,如何停止 Firebase 通知
问题描述
Is there any way to stop showing the push notification when the app is in foreground ?
现在我们正在接收应用程序处于前台或后台的所有消息,但我只想在应用程序处于前台或最小化时显示 Firebase 推送通知。
解决方案
显示通知取决于我们。我们可以配置只在应用程序处于后台时显示通知,并在应用程序处于前台时禁用。我们可以检查应用程序是在前台还是在后台,如下所示。
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,则不显示通知。
推荐阅读
- selenium - 错误 selenium.common.exceptions.ElementNotInteractableException:消息:元素不可交互
- javascript - 我无法通过 post 路由输入对象,而只能在节点 js 中未定义
- mongodb - TypeError:无法使用nodejs在monogdb中读取null的属性“项目”
- css - 如何使 Div(紫色框)容器中的内容响应
- spring-mvc - 如何在spring mvc中将输入流(图像)显示到jsp?
- java - 没有文件的角度上传文件到后端(400错误请求)
- react-native - 简单示例 Expo / React Navigation 5
- swift - 从 var-args 参数创建节点对象
- arrays - 如果具有该值的散列不存在,则在数组中添加散列,否则扩展现有的散列
- c - 来自完全相同的二进制文件的不同输出