2013-10-11 73 views
0

我想創建類似即時消息應用程序的東西。如何在一個通知中顯示多條消息?我可以創建用戶收到單個通知時顯示的通知。但是,當用戶收到多條消息時,如何使用之前的消息更新通知?如果用戶沒有取消通知,我應該將郵件保存到數據庫中並顯示出來嗎?或者還有其他方法可以解決這個問題嗎?通知收件箱樣式

以下是我的通知代碼。

NotificationManager notificationManager = (NotificationManager) 
      context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(icon, "IMTest- A new event is created" , when); 
    Intent notificationIntent = new Intent(context, IM_Chat.class); 
    notificationIntent.putExtra("topicId", topicId); 
    notificationIntent.putExtra("sender", sender); 

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
      Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent intent = 
      PendingIntent.getActivity(context, 1, notificationIntent, Intent.FLAG_ACTIVITY_MULTIPLE_TASK | PendingIntent.FLAG_CANCEL_CURRENT); 
    notification.setLatestEventInfo(context, topicName, "A new event ["+eventName+"] is added in "+topicName, intent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS; 
    notification.defaults |= Notification.DEFAULT_SOUND; 
    notification.ledARGB |= 0xff0000ff; 
    notification.ledOffMS |= 1000; 
    notification.ledOnMS |= 300; 
    notificationManager.notify(CommunitiesappConstant.NOTIFICATION_ID, notification); 

回答

1

可以使用相同的ID和建設者

http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Updating

private NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); 
    private mNotificationManager = 
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    //Different Id's will show up as different notifications 
    private int mNotificationId = 1;  
    //Some things we only have to set the first time. 
    private boolean firstTime = true; 

    private updateNotification(String message, int progress) { 
    if (firstTime) { 
     mBuilder.setSmallIcon(R.drawable.icon) 
     .setContentTitle("My Notification") 
     .setOnlyAlertOnce(true); 
     firstTime = false; 
    } 
    mBuilder.setContentText(message) 
    .setProgress(100, progress, true); 
    mNotificationManager.notify(mNotificationId, mBuilder.build()); 
    } 
+0

感謝信息更新通知。我會嘗試一下!但是,在我更新之前,如何才能檢查通知是否存在? –

+0

你叫notify() – QuokMoon

+1

我試過這個,我只設法爲一條消息創建一個緊湊的通知。我如何實現這個收件箱通知樣式? –