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

时间:2013-11-02 05:54:38

标签: android notifications 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());  

2 个答案:

答案 0 :(得分: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); 

要在点击时关闭通知托盘,请在构建通知时将setAutoCancel()与true联系起来:mb.setAutoCancel(true);

答案 1 :(得分:1)

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

遵循与接受的答案相同的步骤,但是更新此行:

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

另请参阅PendingIntent.FLAG_UPDATE_CURRENT