【问题标题】:Dismiss Ongoing Android Notification Via Action Button Without Opening App在不打开应用程序的情况下通过操作按钮关闭正在进行的 Android 通知
【发布时间】:2013-11-02 05:54:38
【问题描述】:

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

【问题讨论】:

  • 你能不能说的更清楚你想要什么?用户不能关闭正在进行的通知,因此您的应用程序或服务必须注意取消它们。或者,正如您所说,使用 BroadcastReceiver 绑定到操作按钮来执行此操作。 BroadcastReceiver 只需要在传递给它的通知 id 上调用 cancel()。您还需要取消/完成引发通知的事件
  • 我想更一般地说,我只是希望能够在不打开应用程序的情况下按下通知按钮来做某事。那么我将如何实现 BroadcastReceiver 来执行任何操作,我试图阅读的教程非常不清楚。

标签: android notifications broadcastreceiver


【解决方案1】:

从这里开始:

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

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

【讨论】:

  • 感谢您的代码,它按照它所说的做了,但我面临一个额外的问题。该代码会关闭通知,但通知托盘保持打开状态。我如何务实地隐藏/关闭它?
  • @mudit 在构建通知时将setAutoCancel 设置为true:mb.setAutoCancel(true);
  • onReceive 在我的情况下从未被调用
  • 我完全按照你的例子,但我忘了在清单中注册广播接收器:它现在可以工作了!
  • 谢谢,它帮助了我:)
【解决方案2】:

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

按照与接受的答案相同的步骤,但更新这一行:

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

另见PendingIntent.FLAG_UPDATE_CURRENT

【讨论】:

  • 我认为这仅在使用与最初用于显示通知的接收器相同的接收器时才需要,因为在这种情况下 PendingIntent 已经存在?无论如何它对我有用。
  • 谢谢。这节省了我的时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
  • 1970-01-01
  • 1970-01-01
  • 2019-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多