0

私はAndroidアプリケーションでFirebase通知を使用しています。コードの下に使用 生成通知:Androidで1つではなく複数の通知を受け取る

public class FirebaseNotificationService extends FirebaseMessagingService { 

private static final String TAG = FirebaseNotificationService.class.getSimpleName(); 
private String strTitle = "My Notification"; 
private String strMessage = "No Description"; 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    if (Prefrences.getBooleanValue(getApplicationContext(), IS_LOGIN) && Prefrences.getBooleanValue(getApplicationContext(), Prefrences.IS_NOTIFICATION)) { 
     if (remoteMessage.getData().get("title") != null) { 
      strTitle = remoteMessage.getData().get("title"); 
     } 
     if (remoteMessage.getData().get("message") != null) { 
      strMessage = remoteMessage.getData().get("message"); 
     } 
     sendNotification(strTitle, strMessage); 
    } 
} 

private void sendNotification(String strTitle, String messageBody) { 
    try { 
     if (!getTopActivity().equals("com.app.activity.NotificationActivity")) { 
      Intent intent = new Intent(this, DashboardActivity.class); 
      intent.putExtra("notification", "yes"); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
        PendingIntent.FLAG_ONE_SHOT); 

      Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
        .setSmallIcon(R.mipmap.app_icon) 
        .setContentTitle(strTitle) 
        .setContentText(messageBody) 
        .setAutoCancel(true) 
        .setSound(defaultSoundUri) 
        .setContentIntent(pendingIntent); 

      NotificationManager notificationManager = 
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

      notificationManager.notify(++NOTIFICATION_ID /* ID of notification */, notificationBuilder.build()); 
     } else { 
      Intent intent = new Intent(); 
      intent.setAction("load_service"); 
      sendBroadcast(intent); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

public String getTopActivity() { 
    ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); 
    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1); 
    return taskInfo.get(0).topActivity.getClassName(); 
} 
} 

問題は、ステータスバー上の複数の通知は、バックエンドから送信された同じ通知のために、代わりに単一の生成です。

私は定数ファイルでNOTIFICATION_IDを静的な整数として取りました。 と私はdiffernt通知のためにそれを増分しています。

何が問題なのですか?

+0

同じ通知が複数の時間に受信され、1つしか表示されない場合、私は仮定しています。受信した通知のタイプが異なると複数のメッセージが表示されます。正しい? – Raja

答えて

1

同じ通知を受け取ったときに通知IDを増やさないでください。受信した通知が同じか異なる通知かどうかを確認するフラグを設定する必要があります。 FCMからの通知を送信するときに、通知IDを持つキーと値のペアを持つ追加のペイロードを送信します。コードから増やすのではなく通知IDを設定してください。希望すると助けてくれる

関連する問題