我在覆盖 Firebase 推送消息默认行为时遇到了同样的问题(仅在后台显示通知)我通过查看 .class 文件 com.google.firebase.messaging.zzb:53(firebase-messaging :19.0.1) 在我们看来是 getRunningAppProcesses。请注意,FireBase 是由 Google 他们自己创建的。所以我假设它非常节省使用。清理版:
List<ActivityManager.RunningAppProcessInfo> runningApps;
boolean isInForeground =false;
if ((runningApps = ((ActivityManager)this.getApplication().getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses()) != null) {
Iterator runningApp = runningApps.iterator();
int myPid = Process.myPid();
while(runningApp.hasNext()) {
ActivityManager.RunningAppProcessInfo processInfo;
if ((processInfo = (ActivityManager.RunningAppProcessInfo)runningApp.next()).pid == myPid) {
isInForeground = processInfo.importance == 100;
break;
}
}
}