打开活动

时间:2015-10-05 16:06:34

标签: android notifications

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

通知类如下所示:

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

App in the backtround with notification drawer on top

我还有一个基本活动,可在启动活动时删除通知。我错过了什么?

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

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

答案 1 :(得分:0)

Prathibhas suggested solution did not the trick, but pointed me in the right direction. The trick was to send the broadcast

sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));

since I'm using a broadcast receiver for the actions on the notification. The answer was provided here:

Clicking Android Notification Actions does not close Notification drawer