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

问题描述 投票:16回答:2

我有一个应用程序,持续通知,以帮助记忆。我希望能够通过其中一个操作按钮关闭此通知,但我不想在按下按钮时打开应用程序。我更喜欢使用内置的通知操作按钮,而不是创建一个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());  
android notifications broadcastreceiver
2个回答
67
投票

从这开始:

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

创建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);
    }
}  

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

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

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


1
投票

已接受的解决方案无法在Android 8.1及更高版本中运行。

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

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

另见PendingIntent.FLAG_UPDATE_CURRENT

© www.soinside.com 2019 - 2024. All rights reserved.