Show push notification only when app is not running android

后端 未结 2 1461
抹茶落季
抹茶落季 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 taskInfo = am.getRunningTasks(1);
    
            ComponentName componentInfo = taskInfo.get(0).topActivity;
            if (componentInfo.getPackageName().equalsIgnoreCase("YourPackage")) {
                return true;
            } else {
               return false;
            }
      } 
    

提交回复
热议问题