2017-02-18 4 views
-3

私はプロジェクトに取り組んでいます。私はwakefulBroadcastReceiverでインテントサービスを使ってアラームをスケジュールし、通知を送信します。これまでのところ問題はありません。しかし、私は私のアプリを開くときに、それは再び通知を表示しませんしたい。この問題はどうか考えてください。通知Android:アプリが開いているときに通知を表示しない?

答えて

0

通知を受信したときに、そのアプリがバックグラウンドであるかどうかを確認し、それに応じて通知を表示するかどうかを判断できます。

アプリがバックグラウンドかどうかを確認するコードは次のとおりです。

public class MyGcmPushReceiver extends GcmListenerService { 

    /** 
    * Called when message is received. 
    * @param from SenderID of the sender. 
    * @param bundle Data bundle containing message data as key/value pairs. 
    *    For Set of keys use data.keySet(). 
    */ 
    @Override 
    public void onMessageReceived(String from, Bundle bundle) { 
     // Check here whether the app is in background or running. 
     if(isAppIsInBackground(getApplicationContext())) { 
      // Show the notification 
     } else { 
      // Don't show notification 
     } 
    } 

     /** 
     * Method checks if the app is in background or not 
     */ 
     private boolean isAppIsInBackground(Context context) { 
      boolean isInBackground = true; 

      ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 
      if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) { 
       List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses(); 
       for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) { 
        if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { 
         for (String activeProcess : processInfo.pkgList) { 
          if (activeProcess.equals(context.getPackageName())) { 
           isInBackground = false; 
          } 
         } 
        } 
       } 
      } 
      else 
      { 
       List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1); 
       ComponentName componentInfo = taskInfo.get(0).topActivity; 
       if (componentInfo.getPackageName().equals(context.getPackageName())) { 
        isInBackground = false; 
       } 
      } 
      return isInBackground; 
     } 
} 
+0

私は上のメソッドを呼び出しますonCreate activity ?? @氏。ウサギ –

+0

はいアクティビティからの呼び出しですが、これをアプリケーションクラスに投稿 – user3040153

+0

@FinnD onCreateではなく、受信側でこのメソッドを呼び出す必要があります。 GCM受信機の例を示す答えを編集しました。 –

関連する問題