2011-03-14 53 views
3

我目前正在調查android通知。有沒有什麼辦法可以將所有來自應用程序的通知顯示爲一個單獨的android通知窗口中的數字。點擊此按鈕會將用戶重定向到應用程序中的單獨通知視圖。還有什麼辦法來設置每個通知的活動,以便用戶將被重定向到基於點擊通知的適當活動。Android通知管理器幫助

回答

6

您可以附上一個custom layout to your notification,其中包含一個計數器。並且當事件發生時增加計數器和update the notification。類似像下面的例子中:

Notification notification = new Notification(R.drawable.logo, name, System.currentTimeMillis()); 

    RemoteViews contentView = new RemoteViews(appContext.getPackageName(), R.layout.custom_layout); 

    contentView.setTextViewText(R.id.notification_text, Counter); 

notification.contentView = contentView; 

要將活動連接到通知:

 Intent notificationIntent = new Intent(appContext, MyActivity.class); 
     Bundle bundle = new Bundle(); 
     bundle.putInt(...); 
     notificationIntent.putExtras(bundle); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);   

     PendingIntent contentIntent = PendingIntent.getActivity(appContext, (int)System.currentTimeMillis(), notificationIntent, 0); 
     notification.contentIntent = contentIntent; 

希望這將有助於。

+1

謝謝ackio。我會檢查這一點。如果有android的任何默認功能將會更好。 我已經通過這個:http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Updating 他們沒有解釋關於更新通知。 – 2011-03-17 12:44:57

+1

更新僅僅意味着使用相同的ID調用NotificationManager :: notify。如果ID不同,則會添加新通知,否則會更新現有通知。 – ackio 2011-03-18 04:27:45

+0

在那篇文章中,我讀過這一行「消息傳遞應用程序更新現有通知以顯示收到的新消息總數」。所以我相信它是可能的。 – 2011-03-18 06:51:17