2017-10-20 16 views
0

Ici, je suis en train d'afficher des messages de discussion dans des groupes comme WhatsApp dans Android 7.0 ou plus, mais ils ne sont pas affichés comme ça. J'envoie des notifications push locales en obtenant des messages de chat de Node.js. Dans android, j'utilise socket client pour les récupérer et envoyer des notifications push à l'autre utilisateur si leurs applications sont en arrière-plan. Plusieurs messages vont dans un groupe mais quand j'essaye de créer un nouveau groupe quand quelqu'un d'autre envoie des messages, il ajoute dans un même groupe.Impossible d'ajouter le même message Utilisateurs dans le même groupe de notifications Push Nougat Android que whatsapp

Below is the screenshot

Il met à jour juste le titre de notification push que je fais ici:

public static void bundledNotification(String message, String name, String uId) { 
    Context context = ApplicationClass.context(); 
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); 
    ++numberOfBundled; 
    issuedMessages.add(message); 

    //Build and issue the group summary. Use inbox style so that all messages are displayed 
    NotificationCompat.Builder summaryBuilder = new NotificationCompat.Builder(context) 
      .setContentTitle(name) 
      .setContentText(message) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setGroupSummary(true) 
      .setGroup(uId); 

    NotificationCompat.InboxStyle inboxStyle = 
      new NotificationCompat.InboxStyle(); 
    inboxStyle.setBigContentTitle("Messages:"); 
    for (CharSequence cs : issuedMessages) { 
     inboxStyle.addLine(cs); 
    } 
    summaryBuilder.setStyle(inboxStyle); 

    notificationManager.notify(NOTIFICATION_BUNDLED_BASE_ID, summaryBuilder.build()); 

    //Pending Intent 
    Intent notificationIntent = new Intent(context, ChatMessageActivity.class); 
    notificationIntent.putExtra("uid", Integer.parseInt(uId)); 
    notificationIntent.putExtra(context.getString(R.string.notify_id), Integer.parseInt(uId)); 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
    // Adds the back stack 
    stackBuilder.addParentStack(ChatMessageActivity.class); 
    // Adds the Intent to the top of the stack 
    stackBuilder.addNextIntent(notificationIntent); 
    PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

    //issue the Bundled notification. Since there is a summary notification, this will only display 
    //on systems with Nougat or later 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
      .setContentTitle(name) 
      .setContentText(message) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentIntent(contentIntent) 
      .setGroupSummary(true) 
      .setStyle(new NotificationCompat.BigTextStyle() 
        .bigText(name)) 
      .setGroup(uId); 

    //Each notification needs a unique request code, so that each pending intent is unique. It does not matter 
    //in this simple case, but is important if we need to take action on a specific notification, such as 
    //deleting a message 
    int requestCode = NOTIFICATION_BUNDLED_BASE_ID + numberOfBundled; 
    if (messagesMap.get(uId) != null) { 
     messagesMap.get(uId).add(message); 
    } else { 
     ArrayList<String> messageList = new ArrayList<String>(); 
     messageList.add(message); 
     messagesMap.put(uId, messageList); 
     notificationManager.notify(NOTIFICATION_BUNDLED_BASE_ID + numberOfBundled, builder.build()); 
    } 

    AppPreferences.setChatNotificationNumber(context, AppUtils.PUSH_CHATS_NOTIFICATION_NUMBER, requestCode); 
    NotificationCompat.Builder childBuilder = new NotificationCompat.Builder(context) 
      .setContentTitle(name) 
      .setContentText(message) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentIntent(contentIntent) 
      .setStyle(new NotificationCompat.BigTextStyle() 
        .bigText(message)) 
      .setGroup(uId); 

    notificationManager.notify(requestCode, 
      childBuilder.build()); 

} 

De code ci-dessus, il est toujours créer une notification plus à la première fois et la deuxième fois, il commence à ajouter messages dans l'ancienne notification. Mais quand j'essaye d'ajouter de vieux messages dans le fil du même utilisateur il crée un nouveau message sous le groupe principal de la notification. Merci d'avance.

Répondre

0

Utiliser la même setGroup et la notification (NOTIFICATION_BUNDLED_BASE_ID) id (groupe sage)

Context context = ApplicationClass.context(); 
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); 
    ++numberOfBundled; 
    issuedMessages.add(message); 

    //Build and issue the group summary. Use inbox style so that all messages are displayed 
    NotificationCompat.Builder summaryBuilder = new NotificationCompat.Builder(context) 
      .setContentTitle(name) 
      .setContentText(message) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setGroupSummary(true) 
      .setGroup(1001); 

    NotificationCompat.InboxStyle inboxStyle = 
      new NotificationCompat.InboxStyle(); 
    inboxStyle.setBigContentTitle("Messages:"); 
    for (CharSequence cs : issuedMessages) { 
     inboxStyle.addLine(cs); 
    } 
    summaryBuilder.setStyle(inboxStyle); 

    notificationManager.notify(1001, summaryBuilder.build()); 
+0

Son travail pour la notification de base. Maintenant, j'ajoute une nouvelle notification pour la partie enfant. Veuillez vérifier la capture d'écran ci-jointe –