2010-08-13 86 views
28

我已經閱讀了許多如何創建通知消息的示例。 我想達到的目的是因爲通知會由一個小部件執行,我想在點擊時通知用戶的意圖是當用戶點擊它時自行清除它。我沒有要返回的活動。 我的目的通知只是明確地通知,沒有別的。 那麼,一個意圖的代碼是什麼,只是清除/取消自己。 下面的代碼是一個由按鈕啓動的活動(不包括按鈕代碼),通知將由後臺服務啓動。Android通知意圖清除它自己

CharSequence title = "Hello"; 
CharSequence message = "Hello, Android!"; 
final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
final Notification notification = new Notification(R.drawable.icon,"A New Message!",System.currentTimeMillis()); 

notification.defaults=Notification.FLAG_ONLY_ALERT_ONCE+Notification.FLAG_AUTO_CANCEL; 
Intent notificationIntent = new Intent(this, AndroidNotifications.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0); 

notification.setLatestEventInfo(AndroidNotifications.this, title,message, pendingIntent); 
notificationManager.notify(NOTIFICATION_ID, notification); 

感謝

回答

1

我可以看到這樣做是讓你的NotificationIntent點背景Service的唯一途徑。此服務啓動後,將使用NotificationManager.cancel(int id)清除給定的NotificationService然後會自行停止。這不太好,也不容易實現,但我找不到任何其他的方式。

+0

如果您希望通過代碼取消通知,請在取消呼叫時使用「NOTIFICATION_ID」。 – Pentium10 2010-08-13 18:17:21

+0

是的,我會從一個按鈕調用它,但是我想在用戶選擇通知時從代碼中調用它。 – John 2010-08-14 09:18:06

+0

工作爲我解決方案,但有點混亂(額外的服務要寫)。但是在服務中,我可以訪問調用通知ID作爲參數的Intent。 – 2012-02-20 15:17:56

73

退房FLAG_AUTO_CANCEL

位被按位或爲,如果當用戶點擊該通知應取消應設置標誌字段。

編輯:

notification.flags |= Notification.FLAG_AUTO_CANCEL; 
+0

我的代碼已經在使用FLAG_AUTO_CANCEL,但實際上通知不會在點擊時消失。 – John 2010-08-14 09:16:31

+17

您的代碼使用錯誤(放入默認字段),您需要按位或在標誌字段中,如下所示:'notification.flags | = Notification.FLAG_AUTO_CANCEL;' – Pentium10 2010-08-14 16:11:26

+2

Thanks notification.flags | = Notification.FLAG_AUTO_CANCEL;工作。 – John 2010-08-15 09:04:39

19

設置標誌在notification.flags而不是notification.defaults。

例子:

notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL; 
1
/** 
     Post a notification to be shown in the status bar. 
     Obs.: You must save this values somewhere or even pass it as an extra through Intent to use it later 
*/ 
notificationManager.notify(NOTIFICATION_ID, notification); 

/** 
     Cancel a previously shown notification given the notification id you've saved before 
*/ 
notificationmanager.cancel(NOTIFICATION_ID); 
+0

'notify()'導致通知沒有被顯示。 – Aryo 2014-02-08 04:57:02

10

如果您使用NotificationCompat.Builder(的android.support.v4部分),那麼簡單地調用它的對象的方法setAutoCancel

NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
builder.setAutoCancel(true); 

一些人被報告說setAutoCancel()沒有爲他們工作,所以你可以嘗試這種方式以及

builder.build().flags |= Notification.FLAG_AUTO_CANCEL; 
+0

謝謝你的回答:) – Nevaeh 2014-11-24 11:13:29

1

使用setContentIntent應該解決您的問題:

.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0)); 

例如:

NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this) 
     .setSmallIcon(R.drawable.notification_icon) 
     .setContentTitle("title") 
     .setAutoCancel(true) 
     .setContentText("content") 
     .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0)); 
NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
notificationManager.notify(0, mBuilder.build()); 

通常,您可能希望將用戶定向到相關的內容,因此可能會取代「新意圖()」用別的東西。