Show push notification only when app is not running android

后端 未结 2 1463
抹茶落季
抹茶落季 2020-12-30 10:06

In my application I have push notification integrated with GCM. Its working fine whenever notification comes its appearing. But push notification come even when

相关标签:
2条回答
  • 2020-12-30 10:31

    Do something like this to check whether you app is in foreground or not by calling below method and depending on returning value true or false do your rest of work

    public boolean checkApp(){
           ActivityManager am = (ActivityManager) this
                    .getSystemService(ACTIVITY_SERVICE);
    
            // get the info from the currently running task
            List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    
            ComponentName componentInfo = taskInfo.get(0).topActivity;
            if (componentInfo.getPackageName().equalsIgnoreCase("YourPackage")) {
                return true;
            } else {
               return false;
            }
      } 
    
    0 讨论(0)
  • 2020-12-30 10:36

    Kotlin

     fun isAppRunning(context: Context, packageName: String): Boolean {
        val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val procInfos = activityManager.runningAppProcesses
        if (procInfos != null) {
            for (processInfo in procInfos) {
                if (processInfo.processName == packageName) {
                    return true
                }
            }
        }
        return false
    }
    
    0 讨论(0)
提交回复
热议问题