2013-06-03 18 views
1

我用下面的代碼來顯示通知如何在時間的Android應用程序

private void SendNotification(String notificationTitle, String notificationMessage) 
{ 
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
android.app.Notification notification = new android.app.Notification(R.drawable.ic_launcher, notificationMessage, 
     System.currentTimeMillis()); 

Intent notificationIntent = new Intent(this, MainActivity.class); 
notificationIntent.putExtra("notif_id", "5100"); 

//Setting the single top property of the notification and intent. 
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS; 
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); 

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
notification.setLatestEventInfo(MainActivity.this, notificationTitle, notificationMessage, pendingIntent); 
notificationManager.notify((int)System.currentTimeMillis(), notification); 
} 

每當有新通知來只顯示一個通知,前一個應該被刪除,新的一應所示。

任何線索都會有所幫助。

Thanx in advance

o。

+0

要取消前一個(如果存在的話),PendingIntent.getActivity(此,0,notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT),以取代額外只有PendingIntent.FLAG_UPDATE_CURRENT – AlexBcn

+0

AlexBcn,嘗試了兩種選擇,他們都沒有刪除以前的消息。感謝您的迴應。 – osum

回答

1

在發佈新應用程序之前,有一種方法可以清除應用程序的所有通知。 給它一個鏡頭一樣,

notificationManager.cancelAll(); 

要清除一個特定的通知,你可以不喜歡它,

notificationManager.cancel(notification_id); 
+0

這解決了它,thanx hopetribe,但只是想知道是否有任何選項只使用標誌來做到這一點。 – osum

3

每次你generatting用不同的ID的通知(System.currentTimeMillis的()),將其更改爲單個ID。

notificationManager.notify(0, notification);

發佈一個通知在狀態欄顯示。如果具有相同ID的通知已由您的應用程序發佈並且尚未取消,它將被更新的信息替換。

NotificationManager.html

相關問題