2016-07-15 16 views
0

FirebaseMessagingServiceクラスでアプリがフォアグラウンドになっているときに通知を正常に処理しました。これは、このコードを使用して、アプリからの通知を生成します。アプリがファイアベースのバックグラウンドにあるときの通知回数の処理方法

private void sendNotification(String type, String typeID, String messageBody, String title) { 

    Intent intent = new Intent(this, SplashScreenActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(getNotificationIcon()) 
      .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary)) 
      .setContentTitle(getString(R.string.app_name)) 
      .setContentText(messageBody) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
    try { 
     count = MazkaraPref.getInstance().getInt(getApplicationContext(), MazkaraPref.PreferenceKey.notification_count, 0); 
     count++; 
     Log.e("count", ":" + count); 
     MazkaraPref.getInstance().saveInt(getApplicationContext(), MazkaraPref.PreferenceKey.notification_count, count); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

アプリケーションがフォアグラウンドで実行されているとき、私は通知回数を扱ってきたこの道を。しかし、アプリがバックグラウンドにあるとき、私たちは通知やカウント通知を生成するコントロールがありません。したがって、ステータスバーに保留中の通知の数や、SplashScreenを開いたときに通知が届いた回数を知りたい場合は、どのように私はこの通知カウントを達成することができます。私は上記のコードを試したが、アプリケーションが実行されている場合に動作します。保留中の通知数を取得する方法がわかりません。

答えて

0

FirebaseMessagingServiceのonMessageReceivedメソッドでカウントを計算できます。これは、アプリがバックグラウンドの場合でも呼び出されます。

+0

いいえ、「https://firebase.google.com/docs/notifications/android/console-device」を読んで、アプリがバックグラウンドで既に試されているときは、onMessageReceived()を呼び出していません。 @Anandroid –

関連する問題