点击操作后如何解除通知

由于API级别16(果冻豆),有可能添加行动的通知与

builder.addAction(iconId, title, intent); 

但是当我给通知添加一个动作并按下动作时,通知就不会被解除。 当通知本身被点击时,它可以被解雇

 notification.flags = Notification.FLAG_AUTO_CANCEL; 

要么

 builder.setAutoCancel(true); 

但显然,这与通知相关的操作没有任何关系。

任何提示? 还是这不是API的一部分呢? 我没有find任何东西。

当您在通知pipe理器上调用通知时,您为其提供了一个ID – 这是以后可以用来访问它的唯一ID(这是来自通知pipe理器:

 notify(int id, Notification notification) 

要取消,你可以打电话给:

 cancel(int id) 

与相同的ID。 所以,基本上,你需要跟踪id或者可能把idjoin到你添加到PendingIntent中的Intent的Bundle中?

在使用棒棒糖头像显示通知时发现这是一个问题。 请参阅devise指南 。 这是完整的(ISH)代码来实现。

到目前为止,有一个“解雇”button不那么重要,但现在它更在你的脸上。

领导通知

build立通知

 int notificationId = new Random().nextInt(); // just use a counter in some util class... PendingIntent dismissIntent = NotificationActivity.getDismissIntent(notificationId, context); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setPriority(NotificationCompat.PRIORITY_MAX) //HIGH, MAX, FULL_SCREEN and setDefaults(Notification.DEFAULT_ALL) will make it a Heads Up Display Style .setDefaults(Notification.DEFAULT_ALL) // also requires VIBRATE permission .setSmallIcon(R.drawable.ic_action_refresh) // Required! .setContentTitle("Message from test") .setContentText("message") .setAutoCancel(true) .addAction(R.drawable.ic_action_cancel, "Dismiss", dismissIntent) .addAction(R.drawable.ic_action_boom, "Action!", someOtherPendingIntent); // Gets an instance of the NotificationManager service NotificationManager notifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); // Builds the notification and issues it. notifyMgr.notify(notificationId, builder.build()); 

NotificationActivity

 public class NotificationActivity extends Activity { public static final String NOTIFICATION_ID = "NOTIFICATION_ID"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.cancel(getIntent().getIntExtra(NOTIFICATION_ID, -1)); finish(); // since finish() is called in onCreate(), onDestroy() will be called immediately } public static PendingIntent getDismissIntent(int notificationId, Context context) { Intent intent = new Intent(context, NotificationActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.putExtra(NOTIFICATION_ID, notificationId); PendingIntent dismissIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); return dismissIntent; } } 

AndroidManifest.xml (防止SystemUI聚焦到后端堆栈所需的属性)

 <activity android:name=".NotificationActivity" android:taskAffinity="" android:excludeFromRecents="true"> </activity> 

我发现当你在扩展通知中使用动作button时,你必须编写额外的代码,而且你更受限制。

当用户点击一个操作button时,您必须手动取消通知。 该通知仅在默认操作中被自动取消。

此外,如果您从button启动广播接收器,通知抽屉不会closures。

我最终创build了一个新的NotificationActivity来解决这些问题。 没有任何用户界面的中间活动会取消通知,然后启动我真正想从通知开始的活动。

我已经在相关post中发布了示例代码。 单击Android通知操作不会closures通知抽屉 。

在我的调查中,使用BroadcastReceiver是取消通知的一种更简洁的方法:

在AndroidManifest.xml中:

 <receiver android:name=.NotificationCancelReceiver" > <intent-filter android:priority="999" > <action android:name="com.example.cancel" /> </intent-filter> </receiver> 

在java文件中:

 Intent cancel = new Intent("com.example.cancel"); PendingIntent cancelP = PendingIntent.getBroadcast(context, 0, cancel, PendingIntent.FLAG_CANCEL_CURRENT); NotificationCompat.Action actions[] = new NotificationCompat.Action[1]; NotificationCancelReceiver public class NotificationCancelReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //Cancel your ongoing Notification }; } 

您可以始终从操作调用的任何东西(例如,在与提供给addAction()PendingIntent绑定的活动的onCreate()cancel() Notification