打开时隐藏通知抽屉 Activity

Hide Notification Drawer when opening Activity

我收到一条通知,当我点击它时,会打开我的应用程序。但是我的应用程序在后台打开并且通知抽屉仍然可见。我的通知本身已被取消并删除,但抽屉仍然存在于所有内容之上。

通知 class 如下所示:

 public MyNotification(final Context context) {
    this.context = context;

    remoteView = new RemoteViews(context.getPackageName(), R.layout.notification);

    notification = new Builder(context)
            .setSmallIcon(R.drawable.notification_icon)
            .build();

    notification.flags = Notification.FLAG_NO_CLEAR;
    notification.priority = Notification.PRIORITY_MAX;

    remoteView.setOnClickPendingIntent(R.id.container, getIntent(ACTION_OPEN_APP));

    notification.bigContentView = remoteView;

    notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(MY_NOTIFICATION_ID, notification);
}

private PendingIntent getIntent(String action) {
    Intent receiveIntent = new Intent(context, NotificationReceiver.class);
    receiveIntent.setAction(action);
    return PendingIntent.getBroadcast(context, 0, receiveIntent, 0);
}

我的接收器看起来像这样:

public class NotificationReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if(action.equalsIgnoreCase(AudioPlayerNotification.ACTION_OPEN_APP)) {

        Intent openAppIntent = new Intent(context, MyActivity.class);
        openAppIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        context.startActivity(openAppIntent);
    }
}

我还有一个基础 Activity,可以在启动 activity 时删除通知。我错过了什么?

你可以试试这个:

NotificationManager nmagr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            Notification notification=mbuild.build();
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            nmagr.notify(1,notification);

有关更多信息,请查看以下链接:

Clicking Android Notification Actions does not close Notification drawer

How to dismiss notification after action has been clicked

Prathibhas 建议的解决方案没有成功,但为我指明了正确的方向。诀窍是发送广播

sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));

因为我正在使用广播接收器来执行通知操作。此处提供了答案:

Clicking Android Notification Actions does not close Notification drawer