2014-12-05 17 views
7

Devo formare un gruppo di notifiche. Ho fatto l'esempio enter link description hereLe notifiche di impilamento non funzionano

ma il mio avviso non è raggruppato. che cosa ho fatto di sbagliato?

il mio codice:

private static int id =0; 
    final static String GROUP_KEY_GUEST = "group_key_guest"; 


    private static void generateNotification(Context context, String message) { 

      NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); 

      Intent intent = new Intent(context,MyActivity.class); 
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

      Notification notification = new NotificationCompat.Builder(context) 
        .setContentIntent(pendingIntent) 
        .setSmallIcon(R.drawable.ic_stat_gcm) 
        .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm)) 
        .setTicker("New Message") 
        .setWhen(System.currentTimeMillis()) 
        .setAutoCancel(true) 
        .setContentTitle("Message") 
        .setContentText(message) 
        .setGroup(GROUP_KEY_GUEST) 
        .build(); 

      notificationManager.notify(id++, notification); 
     } 

chiamata di metodo:

@Override 
    protected void onMessage(Context context, Intent intent) { 
     Log.d(TAG, "Поступило сообщение: " + intent.getExtras()); 
     String message = intent.getStringExtra("content"); 
     generateNotification(context, message); 
    } 

EDIT, codice completo: Questo è il servizio che ho generare notifiche

package com.managment.pavel.managmentgradle; 

    public class GCMIntentService extends GCMBaseIntentService { 
     private static int id =0; 
     final static String GROUP_KEY_GUEST = "group_key_guest"; 
     NotificationManagerCompat notificationManager; 
     public GCMIntentService() { 
      super(SENDER_ID); 
     notificationManager = NotificationManagerCompat.from(getApplicationContext()); 
     } 

     @Override 
     protected void onMessage(Context context, Intent intent) { 
      String message = intent.getStringExtra("content"); 
      generateNotification(context, message); 
     } 

     @Override 
     protected void onError(Context context, String s) { 
      Toast.makeText(context,"Error",Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     protected void onRegistered(Context context, String registrationId) { 
      ServerUtilities.register(context, registrationId); 
     } 

     @Override 
     protected void onUnregistered(Context context, String registrationId) { 
      ServerUtilities.unregister(context, registrationId); 
     } 

     private void generateNotification(Context context, String message) { 
      Intent intent = new Intent(context,MyActivity.class); 
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

      Notification notification = new NotificationCompat.Builder(context) 
        .setContentIntent(pendingIntent) 
        .setSmallIcon(R.drawable.ic_stat_gcm) 
        .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm)) 
        .setTicker("Новое сообщение") 
        .setWhen(System.currentTimeMillis()) 
        .setAutoCancel(true) 
        .setContentTitle("Сообщение") 
        .setContentText(message) 
        .setGroup(GROUP_KEY_GUEST) 
        .build(); 

      notificationManager.notify(id++, notification); 
     } 
    } 
+0

Penso che ogni volta che inizializza nuova istanza di NotificationManagerCompat in modo da provare inizializzare solo di un tempo generateNotification lato out(). –

+0

forse stupido ma non posso. GCMIntentService di classe pubblica estende GCMBaseIntentService { private static int id = 0; stringa statica finale GROUP_KEY_GUEST = "group_key_guest"; NotificationManagerCompat notificationManager; public GCMIntentService() { super (SENDER_ID); notificationManager = NotificationManagerCompat.from (getApplicationContext()); } –

risposta

0

Prova inizializzare NotificationManagerCompat in generateNotification se l'istanza di NotificationManagerCompat è null an d rimuovere il costruttore del modulo di inizializzazione del codice di default:

private void generateNotification(Context context, String message) { 
    if(notificationManager==null){ 
     notificationManager = NotificationManagerCompat.from(context); 
    } 

    Intent intent = new Intent(context,MyActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

    Notification notification = new NotificationCompat.Builder(context) 
    .setContentIntent(pendingIntent) 
    .setSmallIcon(R.drawable.ic_stat_gcm) 
    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm)) 
    .setTicker("Новое сообщение") 
    .setWhen(System.currentTimeMillis()) 
    .setAutoCancel(true) 
    .setContentTitle("Сообщение") 
    .setContentText(message) 
    .setGroup(GROUP_KEY_GUEST) 
    .build(); 
    notificationManager.notify(id++, notification); 
} 
+0

Sì, l'errore era sparito, ma l'avviso non era ancora raggruppato ( –

6

È necessario creare una notifica di riepilogo con i dettagli del precedente. Questa notifica di riepilogo è visibile, non la precedente. E 'qualcosa di simile:

private static int id =0; 
private static int unread_notif = 0; 
final static String GROUP_KEY_GUEST = "group_key_guest"; 

private static void generateNotification(Context context, String message) { 

     NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); 

     Intent intent = new Intent(context,MyActivity.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

     Notification notification = new NotificationCompat.Builder(context) 
       .setContentIntent(pendingIntent) 
       .setSmallIcon(R.drawable.ic_stat_gcm) 
       .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm)) 
       .setTicker("New Message") 
       .setWhen(System.currentTimeMillis()) 
       .setAutoCancel(true) 
       .setContentTitle("Message") 
       .setContentText(message) 
       .setGroup(GROUP_KEY_GUEST) 
       .build(); 

     notificationManager.notify(id++, notification); 

     unread_notif++; 

     if (unread_notif>1) { 
      Notification summaryNotification = new NotificationCompat.Builder(mContext) 
       .setContentTitle("Your summary message") 
       .setSmallIcon(R.drawable.ic_small_icon) 
       .setLargeIcon(largeIcon) 
       .setStyle(new NotificationCompat.InboxStyle() 
        .addLine("Details about your first notification") 
        .addLine("Details about your second notification") 
       .setBigContentTitle(Integer.toString(unread_notif)+" new notifications") 
       .setSummaryText("More details in app")) 
       .setGroup(GROUP_KEY_GUEST) 
       .setGroupSummary(true) 
       .build(); 

      notificationManager.notify(id++, summaryNotification); 
     } 
    } 

vedere la documentazione in Add a Summary Notification

+2

Come si prendono i dettagli dalla notifica precedente? – Lion789

+0

Ci scusiamo per il mio cattivo inglese, intendo "dettagli sul precedente", non "dal precedente" Si definisce come farlo, in genere mantengo i dati nel database. –

Problemi correlati