2016-05-31 14 views
7

ecco il mio codice per la notifica. si genera una nuova notifica ogni voltaCome visualizzare più notifiche come gruppo?

Random random = new Random(); 
int m = random.nextInt(9999 - 1000);  
NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(context) 
        .setSmallIcon(R.drawable.quemark1) 
        .setContentTitle("New Message") 
        .setContentText(message) 
Intent intent = new Intent(this, ActivityMain.class); 
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
stackBuilder.addParentStack(ActivityMain.class); 
stackBuilder.addNextIntent(intent); 
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
mBuilder.setContentIntent(resultPendingIntent); 
NotificationManager mNotificationManager = (NotificationManager)  context.getSystemService(Context.NOTIFICATION_SERVICE); 
mBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE); 
mBuilder.setAutoCancel(true); 
mNotificationManager.notify(m, mBuilder.build()); 

here is the output of my code

+0

sì come notifica Whatsapp @Dipali –

+0

thanx @Dipalishah –

risposta

5

Si genera la notifica con messaggi multipli come Gmail

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.quemark1) 
        .setContentTitle("Title") 
        .setContentText("New Message received"); 
      NotificationCompat.InboxStyle inboxStyle = 
        new NotificationCompat.InboxStyle(); 

      inboxStyle.setBigContentTitle("doUdo"); 

      // Add your All messages here or use Loop to generate messages 

       inboxStyle.addLine("Messgare 1"); 
       inboxStyle.addLine("Messgare 2"); 
          . 
          . 
       inboxStyle.addLine("Messgare n"); 


      mBuilder.setStyle(inboxStyle); 
      TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 

      stackBuilder.addNextIntent(intent); 

      PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 
        PendingIntent.FLAG_UPDATE_CURRENT); 

      mBuilder.setContentIntent(pIntent); 

      NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
      mBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE); 
      mBuilder.setAutoCancel(true); 
      mNotificationManager.notify(0, mBuilder.build()); 
+0

ma come aggiungere già esistenti linee di nuovo. Ad esempio, c'erano 4 notifiche (linee) in precedenza, quindi è arrivata una nuova notifica, quindi come aggiungerla nell'ultimo gruppo esistente? – NarendraJi

+0

è sufficiente creare un elenco di notifiche e aggiungere una notifica alla volta nel ciclo inboxStyle.addLine ("Messgare 1"); –

+0

Sì, dovevo salvare le notifiche esistenti localmente, e quindi accedervi e aggiungerne uno nuovo nella stessa lista, ha funzionato per me :) – NarendraJi

5

Quando si crea le notifiche per un dispositivo palmare, si dovrebbe sempre aggregati notifiche simili in una singola notifica sintesi.

Controllare here questo mostra come creare la notifica di stack.

private void sendStackNotificationIfNeeded(RemoteNotification remoteNotification) { 
    // only run this code if the device is running 23 or better 
    if (Build.VERSION.SDK_INT >= 23) { 
     ArrayList<StatusBarNotification> groupedNotifications = new ArrayList<>(); 

     // step through all the active StatusBarNotifications and 
     for (StatusBarNotification sbn : getNotificationManagerService().getActiveNotifications()) { 
      // add any previously sent notifications with a group that matches our RemoteNotification 
      // and exclude any previously sent stack notifications 
      if (remoteNotification.getUserNotificationGroup() != null && 
        remoteNotification.getUserNotificationGroup().equals(sbn.getNotification().getGroup()) && 
        sbn.getId() != RemoteNotification.TYPE_STACK) { 
       groupedNotifications.add(sbn); 
      } 
     } 

     // since we assume the most recent notification was delivered just prior to calling this method, 
     // we check that previous notifications in the group include at least 2 notifications 
     if (groupedNotifications.size() > 1) { 
      NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 

      // use convenience methods on our RemoteNotification wrapper to create a title 
      builder.setContentTitle(String.format("%s: %s", remoteNotification.getAppName(), remoteNotification.getErrorName())) 
        .setContentText(String.format("%d new activities", groupedNotifications.size())); 

      // for every previously sent notification that met our above requirements, 
      // add a new line containing its title to the inbox style notification extender 
      NotificationCompat.InboxStyle inbox = new NotificationCompat.InboxStyle(); 
      { 
       for (StatusBarNotification activeSbn : groupedNotifications) { 
        String stackNotificationLine = (String)activeSbn.getNotification().extras.get(NotificationCompat.EXTRA_TITLE); 
        if (stackNotificationLine != null) { 
         inbox.addLine(stackNotificationLine); 
        } 
       } 

       // the summary text will appear at the bottom of the expanded stack notification 
       // we just display the same thing from above (don't forget to use string 
       // resource formats!) 
       inbox.setSummaryText(String.format("%d new activities", groupedNotifications.size())); 
      } 
      builder.setStyle(inbox); 

      // make sure that our group is set the same as our most recent RemoteNotification 
      // and choose to make it the group summary. 
      // when this option is set to true, all previously sent/active notifications 
      // in the same group will be hidden in favor of the notifcation we are creating 
      builder.setGroup(remoteNotification.getUserNotificationGroup()) 
       .setGroupSummary(true); 

      // if the user taps the notification, it should disappear after firing its content intent 
      // and we set the priority to high to avoid Doze from delaying our notifications 
      builder.setAutoCancel(true) 
       .setPriority(NotificationCompat.PRIORITY_HIGH); 

      // create a unique PendingIntent using an integer request code. 
      final int requestCode = (int)System.currentTimeMillis()/1000; 
      builder.setContentIntent(PendingIntent.getActivity(this, requestCode, DetailActivity.createIntent(this), PendingIntent.FLAG_ONE_SHOT)); 

      Notification stackNotification = builder.build(); 
      stackNotification.defaults = Notification.DEFAULT_ALL; 

      // finally, deliver the notification using the group identifier as the Tag 
      // and the TYPE_STACK which will cause any previously sent stack notifications 
      // for this group to be updated with the contents of this built summary notification 
      getNotificationManagerService().notify(remoteNotification.getUserNotificationGroup(), RemoteNotification.TYPE_STACK, stackNotification); 
     } 
    } 
} 
-1

Utilizzare di seguito fornire il codice per il gestore delle notifiche e incrementare il conteggio ogni volta che viene ricevuta una nuova notifica.

mNotificationManager.notify(count, mBuilder.build()); 
Problemi correlati