如何使用mBuilder.addAction取消通知

时间:2017-03-06 07:51:44

标签: android notifications android-notifications

我想在通知上添加取消按钮。 在该按钮的单击上,我想从通知栏中删除我的通知。 有没有办法做到这一点,或者我是以错误的方式做到这一点。

 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",);

1 个答案:

答案 0 :(得分:0)

 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);