2016-05-31 11 views
7

Hier ist mein Code für die Benachrichtigung. sie erzeugen eine neue Benachrichtigung jedes MalWie können mehrere Benachrichtigungen als Gruppe angezeigt werden?

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

ja wie WhatsApp Benachrichtigung @Dipali –

+0

thanx @Dipalishah –

Antwort

5

Es wird Benachrichtigung mit mehreren Nachrichten wie Google Mail generieren

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

aber wie bestehende Linien hinzuzufügen schon wieder. Zum Beispiel gab es früher 4 Benachrichtigungen (Zeilen), und dann kam eine neue Benachrichtigung, also wie man diese in der letzten existierenden Gruppe hinzufügt? – NarendraJi

+0

erstellen Sie einfach eine Liste der Benachrichtigungen und fügen Sie eine Benachrichtigung in Schleife inboxStyle.addLine ("Messgare 1"); –

+0

Ja, musste die vorhandenen Benachrichtigungen lokal speichern, und dann auf sie zugreifen und neue in der gleichen Liste hinzufügen, arbeitete für mich :) – NarendraJi

5

Wenn Benachrichtigungen für einen Handheld-Gerät zu schaffen, sollten Sie immer Aggregat ähnliche Meldungen in einer einzigen Sammelbenachrichtigung.

Prüfen Sie here zeigt dies, wie Stapel Benachrichtigung erstellen.

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

Verwenden Sie unten den Code für Ihren Benachrichtigungsmanager und erhöhen Sie die Anzahl, wenn eine neue Benachrichtigung empfangen wurde.

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