2014-12-05 9 views
7

알림 그룹을 구성해야합니다. 예를 만들었습니다 enter link description here스태킹 알림이 작동하지 않습니다.

하지만 내 고지는 그룹화되지 않았습니다. 내가 뭘 잘못 했니?

내 코드 :

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

메서드 호출 :

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

편집, 전체 코드 : 이 내가

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

의 설명서를 참조하십시오(). –

+0

어쩌면 어리 석었지만 나는 할 수 없다. 공용 클래스 GCMIntentService는 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()); –

답변

0

generateNotification에 NotificationManagerCompat를 초기화하십시오 알림을 생성하는 내 서비스 NotificationManagerCompat 인스턴스가 null의 경우 d 초기화 코드 양식 기본 생성자를 제거하십시오.

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

예 오류가 발생했지만 알림이 그룹화되지 않았습니다 ( –

6

이전의 세부 사항과 함께 요약 통지를 작성해야합니다. 이 요약 알림은 이전 알림이 아닌 표시됩니다. 그것은이 같은 뭔가 : 나는 당신이 때마다 너무에만 한때 아웃 사이드 generateNotification를 초기화하려고 NotificationManagerCompat의 새 인스턴스를 초기화 생각

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

Add a Summary Notification

+2

) 이전 알림에서 세부 정보를 얻는 방법 – Lion789

+0

나쁜 영어로 죄송합니다. 이전의 정보가 아니라 이전의 정보를 의미합니다. 당신이 그것을하는 방법을 정의, 나는 일반적으로 데이터베이스에 데이터를 유지합니다. –

관련 문제