web-dev-qa-db-fra.com

Comment grouper les notifications Android comme WhatsApp?

Je ne sais pas comment grouper deux notifications ou plus en une seule et afficher un message du type "Vous avez deux nouveaux messages".

23

Mesures à prendre à partir du code ci-dessous.

NotificationCompat.Builder:contains the UI specification and action information
NotificationCompat.Builder.build() :used to create notification (Which returns Notification object)
Notification.InboxStyle: used to group the notifications belongs to same ID
NotificationManager.notify():to issue the notification.

Utilisez le code ci-dessous pour créer une notification et la regrouper. Inclure la fonction dans un clic de bouton.

private final int NOTIFICATION_ID = 237;
private static int value = 0;
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.Push_notify_icon);
public void buttonClicked(View v)
{
        value ++;
        if(v.getId() == R.id.btnCreateNotify){
            NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            Notification.Builder builder = new Notification.Builder(this);            
            builder.setContentTitle("Lanes");
            builder.setContentText("Notification from Lanes"+value);
            builder.setSmallIcon(R.drawable.ic_launcher);
            builder.setLargeIcon(bitmap);
            builder.setAutoCancel(true);
            inboxStyle.setBigContentTitle("Enter Content Text");
            inboxStyle.addLine("hi events "+value);
            builder.setStyle(inboxStyle);
            nManager.notify("App Name",NOTIFICATION_ID,builder.build());
        }
}

Pour des notifications distinctes, attribuez différents NOTIFICATION_ID.

28
Sackurise

Pour une logique complète, veuillez vérifier ma réponse.J'ai utilisé la logique avec les préférences partagées et le récepteur de diffusion, car j'avais besoin de grouper chaque message d'utilisateur en un message unique et de voir les notifications actives. notifications, cela ne m’aide pas du tout.Alors j’ai décidé d’écrire un peu de logique.Vérifiez-le ici si vous en avez envie.

https://stackoverflow.com/a/38079241/6466619

1
VA Entertaiment

Vous devez créer la notification afin qu'elle puisse être mise à jour avec un ID de notification en appelant NotificationManager.notify(ID, notification)

Les étapes suivantes doivent être créées pour mettre à jour la notification:

  1. Mettre à jour ou créer un objet NotificationCompat.Builder
  2. Construire un objet Notification à partir de celui-ci
  3. Émettez la notification avec le même ID que vous avez utilisé précédemment

Un exemple tiré des documents de développement Android:

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

// Sets an ID for the notification, so it can be updated
int notifyID = 1;

mNotifyBuilder = new NotificationCompat.Builder(this)
    .setContentTitle("New Message")
    .setContentText("You've received new messages.")
    .setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;

// Start of a loop that processes data and then notifies the user
...
mNotifyBuilder.setContentText(currentText).setNumber(++numMessages);

// Because the ID remains unchanged, the existing notification is updated.
mNotificationManager.notify(notifyID, mNotifyBuilder.build());
...

Voir également la documentation Android sur le cumul des notifications https://developer.Android.com/training/wearables/notifications/stacks.html

0
Smittey

Vous pouvez empiler toutes vos notifications dans un seul groupe en utilisant la méthodesetGroupet en transmettant votre chaîne groupId en tant que paramètre.

builer.setGroup ("GROUP ID STRING");

NotificationManager nManager = (NotificationManager) 
getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(this);            
builder.setContentTitle("Lanes");
builder.setGroup("GROUP_ID_STRING");
builder.setContentText("Notification from Lanes"+value);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setLargeIcon(bitmap);
builder.setAutoCancel(true);
inboxStyle.setBigContentTitle("Enter Content Text");
inboxStyle.addLine("hi events "+value);
builder.setStyle(inboxStyle);
nManager.notify("App Name",NOTIFICATION_ID,builder.build());
0
Natesh bhat