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

cjb*_*s12 18 notifications android broadcastreceiver

我有一个应用程序,持续通知,以帮助记忆.我希望能够通过其中一个操作按钮关闭此通知,但我不想在按下按钮时打开应用程序.我更喜欢使用内置的通知操作按钮,而不是创建一个RemoteViews对象来填充通知.我在我的应用程序中看到了一个在这个按钮上使用BroadcastReceiver的帖子提及,虽然他的教程非常无益,但听起来这是朝着正确的方向发展.

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());  
Run Code Online (Sandbox Code Playgroud)

ram*_*ral 69

从这开始:

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());  
Run Code Online (Sandbox Code Playgroud)

创建BroadcastReceiver:

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);
    }
}  
Run Code Online (Sandbox Code Playgroud)

如果您不希望在用户单击通知时显示任何活动,请setContentIntent以这种方式定义意图传递:

PendingIntent resultPendingIntent = PendingIntent.getActivity(context,  0, new Intent(), 0);
......
......
mb.setContentIntent(resultPendingIntent); 
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

  • 我完全按照你的例子,但我忘了在清单中注册广播接收器:它现在有效! (8认同)
  • 感谢代码,它做了它所说的,但我面临一个额外的问题.代码解除通知但通知托盘仍然打开.如何以务实的方式隐藏/关闭它? (3认同)

Yog*_*thi 5

接受的解决方案不适用于 Android 8.1 及更高版本。

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

//Create the PendingIntent
PendingIntent btPendingIntent = PendingIntent.getBroadcast(context, 0, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)

也可以看看PendingIntent.FLAG_UPDATE_CURRENT