简体   繁体   English

如何使用mBuilder.addAction取消通知

[英]How do i cancel notification using mBuilder.addAction

I want to add cancel buttons on notification. 我想在通知上添加取消按钮 On click of that button i want to remove my notification from notification bar. 点击该按钮,我想从通知栏中删除我的通知。 Is there any way to do that or i am doing it in a wrong way. 有没有办法做到这一点,或者我是以错误的方式做到这一点。

 noti=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
      mBuilder=new NotificationCompat.Builder(this);
        mBuilder.setSmallIcon(R.drawable.and);
        mBuilder.setContentTitle("Downloading");
        mBuilder.setContentText("Downloading in progress");
        mBuilder.addAction(R.drawable.and,"cancil",);
 Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_ONE_SHOT);

 //Button as addAction is deprecated use this one.
NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "Cancel", pendingIntent).build();

Notification notification = new NotificationCompat.Builder(this)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentText("Back to Application ?")
        .setContentTitle("Amazing news")
        .addAction(action) //add buton
        .build();

//Send notification
NotificationManager notificationManager = (NotificationManager)
        this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用android.support.v4.app.NotificationCompat.mBuilder.addAction时是否可以清除Notification - Is it possible to clear the Notification when using android.support.v4.app.NotificationCompat.mBuilder.addAction addAction时通知不会自动取消 - Notification not automatically cancel when addAction 如何使用addAction按钮取消正在进行的服务 - How to use addAction button to cancel the ongoing service 如何使.addAction做某事 - How to make .addAction do something 如何让用户取消Android中的进度通知? - How do I let the user cancel a progress notification in Android? 如何在Android中使用builder.addaction(icon,“ string”,pendingIntent)通知方法添加图片? - How to add image using builder.addaction(icon,“string”,pendingIntent) method of notification in android? 使用服务通知时的addAction侦听器 - addAction listener on notification with service 更改通知addAction图标和文本颜色,而无需使用自定义布局 - change notification addAction icon and text color without using custom layouts 如何从notification.addaction获取intent.getExtras以工作? - How to get intent.getExtras from notification.addaction to work? 通知按钮(.addaction)无法启动活动 - notification buttons (.addaction) not launching activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM