2010-08-13 56 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()); 

通常,您可能希望将用户定向到相关的内容,因此可能会取代“新意图()”用别的东西。