在不打开应用程序的情况下,通过操作按钮关闭正在进行的Android通知

在不打开应用程序的情况下,通过操作按钮关闭正在进行的Android通知,android,notifications,broadcastreceiver,Android,Notifications,Broadcastreceiver,我有一个应用程序,有一个持续的通知,以帮助记忆。我希望能够通过一个操作按钮取消此通知,但我不想在按下按钮时打开应用程序。我更喜欢使用内置的通知操作按钮,而不是创建RemoteView对象来填充通知。我看到一篇帖子提到在我的应用程序中收到的这个按钮上使用了广播接收器,虽然他的教程没有什么帮助,但听起来这是朝着正确的方向发展的 Intent resultIntent = new Intent(getBaseContext(), Dashboard.class); TaskStackBuild

我有一个应用程序,有一个持续的通知,以帮助记忆。我希望能够通过一个操作按钮取消此通知,但我不想在按下按钮时打开应用程序。我更喜欢使用内置的通知操作按钮,而不是创建RemoteView对象来填充通知。我看到一篇帖子提到在我的应用程序中收到的这个按钮上使用了广播接收器,虽然他的教程没有什么帮助,但听起来这是朝着正确的方向发展的

Intent resultIntent = new Intent(getBaseContext(), Dashboard.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(getBaseContext());
        stackBuilder.addParentStack(Dashboard.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
                );
    Intent cancel = new Intent(getBaseContext(), CancelNotification.class);
        stackBuilder.addParentStack(Dashboard.class);
        stackBuilder.addNextIntent(cancel);
        PendingIntent pendingCancel =
                stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
                );

    NotificationCompat.Builder mb = new NotificationCompat.Builder(getBaseContext());
        mb.setSmallIcon(R.drawable.cross_icon);
        mb.setContentTitle(ref);
        mb.setContentText(ver);
        mb.setPriority(NotificationCompat.PRIORITY_LOW);
        mb.setOngoing(true);
        mb.setStyle(new NotificationCompat.BigTextStyle().bigText(ver));
        mb.setContentIntent(resultPendingIntent);
        mb.addAction(R.drawable.ic_cancel_dark, "Dismiss", pendingCancel);

    manager.notify(1, mb.build());  
从这个开始:

int final NOTIFICATION_ID = 1;

//Create an Intent for the BroadcastReceiver
Intent buttonIntent = new Intent(context, ButtonReceiver.class);
buttonIntent.putExtra("notificationId",NOTIFICATION_ID);

//Create the PendingIntent
PendingIntent btPendingIntent = PendingIntent.getBroadcast(context, 0, buttonIntent,0);

//Pass this PendingIntent to addAction method of Intent Builder
NotificationCompat.Builder mb = new NotificationCompat.Builder(getBaseContext());
.....
.....
.....
mb.addAction(R.drawable.ic_Action, "My Action", btPendingIntent);
manager.notify(NOTIFICATION_ID, mb.build());  
创建广播接收器:

public class ButtonReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        int notificationId = intent.getIntExtra("notificationId", 0);

        // Do what you want were.
        ..............
        ..............

        // if you want cancel notification
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(notificationId);
    }
}  
如果您不想在用户单击通知时显示任何活动,请通过以下方式定义在
setContentIntent
中传递的意图:

PendingIntent resultPendingIntent = PendingIntent.getActivity(context,  0, new Intent(), 0);
......
......
mb.setContentIntent(resultPendingIntent); 

要在单击时关闭通知托盘,请在生成通知时使用true调用
setAutoCancel()
mb.setAutoCancel(true)

接受的解决方案在Android 8.1及更高版本中不起作用

按照与接受答案中相同的步骤操作,但更新此行:

//Create the PendingIntent
PendingIntent btPendingIntent = PendingIntent.getBroadcast(context, 0, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);

另请参见

您能更清楚地了解您想要的吗?用户无法取消正在进行的通知,因此您的应用程序或服务必须负责取消这些通知。或者,如您所说,使用
BroadcastReceiver
绑定到操作按钮来完成此操作。
BroadcastReceiver
只需要对传递给它的通知id调用cancel()。你还需要取消/完成引发通知的事件。我想更一般地说,我只想能够在不打开应用程序的情况下按下通知按钮来做一些事情。因此,我将如何实现BroadcastReceiver来执行任何操作,我试图阅读的教程非常不清楚。感谢代码,它做到了它所说的,但我面临一个额外的问题。代码将取消通知,但通知托盘仍保持打开状态。如何实际地隐藏/关闭它?@mudit在生成通知时将
setAutoCancel
设置为true:
mb.setAutoCancel(true)
onReceive
在我的案例中从未被调用我完全遵循了你的示例,但我忘记在清单中注册broadcastreceiver:它现在可以工作了!谢谢,它帮助了我:)我认为只有在使用与最初显示通知的接收器相同的接收器时才需要这样做,因为在这种情况下,挂起的内容已经存在了?不管怎样,这对我来说很有效。