2014-12-05 20 views
7

Tôi cần phải tạo thành một nhóm thông báo. Tôi đã tạo ví dụ enter link description hereThông báo xếp chồng không hoạt động

nhưng thông báo của tôi không được nhóm lại. tôi đã làm gì sai?

mã của tôi:

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); 
     } 

gọi phương thức:

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

EDIT, mã đầy đủ: Đây là dịch vụ của tôi mà tôi tạo ra các thông báo

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

Tôi nghĩ rằng bạn mỗi lần khởi tạo thể hiện mới của NotificationManagerCompat vì vậy hãy thử khởi tạo chỉ onetime bên generateNotification ra(). –

+0

có lẽ ngu ngốc nhưng tôi không thể. lớp công khai GCMIntentService mở rộng GCMBaseIntentService { private static int id = 0; Chuỗi tĩnh cuối cùng GROUP_KEY_GUEST = "group_key_guest"; NotificationManagerCompat notificationManager; GCMIntentService công cộng() { siêu (SENDER_ID); notificationManager = NotificationManagerCompat.from (getApplicationContext()); } –

Trả lời

0

Hãy thử khởi NotificationManagerCompat trong generateNotification nếu cá thể NotificationManagerCompat là null d xoá hàm khởi tạo mặc định của biểu mẫu mã:

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

Đã xảy ra lỗi, nhưng thông báo vẫn chưa được nhóm ( –

6

Bạn phải tạo thông báo tóm tắt với các chi tiết từ trước đó. Thông báo tóm tắt này hiển thị, không hiển thị trước đó. Đó là một cái gì đó như thế này:

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); 
     } 
    } 

Xin xem tài liệu trong Add a Summary Notification

+2

Làm cách nào để bạn lấy chi tiết từ thông báo trước? – Lion789

+0

Xin lỗi vì tiếng anh xấu của tôi. Ý tôi là "chi tiết về trước", không phải "từ trước" Bạn xác định làm thế nào để làm điều đó, tôi thường duy trì dữ liệu trong cơ sở dữ liệu. –

Các vấn đề liên quan