0

J'utilise la notification Firebase dans mon application Android. notification Génération en utilisant le code ci-dessous:Obtention de plusieurs notifications au lieu d'une seule dans Android

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(); 
} 
} 

La question est la notification multiple sur la barre d'état génère au lieu de simple, pour la notification même envoyée par le back-end.

J'ai pris NOTIFICATION_ID comme un entier statique dans mon fichier constant. et je l'incrémente pour la notification differnt.

Quel pourrait être le problème?

+0

Je suppose, si la même notification reçoit dans plusieurs temps alors montrez seulement un. mais le type différent de notification recevant alors montrer multiple. est correct? – Raja

Répondre

1

N'augmentez pas l'ID de notification lorsque vous recevez la même notification. vous devez définir un indicateur pour vérifier si la notification reçue est identique ou différente. lorsque vous envoyez une notification de FCM, envoyez une charge utile supplémentaire qui a une paire clé-valeur qui porte l'identifiant de notification. définir cet ID de notification au lieu d'augmenter à partir de votre code. Espérons que ça aide