繁体   English   中英

如何从状态栏中删除通知?

[英]How do I remove Notification from status bar?

我会根据自己的状况在状态栏中显示Notification 到此为止,还可以。

现在我的应用程序中的问题是当我返回到应用程序时, Notification仍显示在状态栏中。

从应用程序回来时,我不希望收到Notification 为此,请给我一些建议。

我没有足够的代表来添加评论,所以@Commonware。 如果您提供了更具体的详细信息,例如告诉他们在创建通知时使用“通知ID”并使用该ID取消通知,则对将来的读者会更有用。 如果您给出答案,请使其完整或至少提供完整的详细信息。 这些帖子不仅用于OP,还可以帮助遇到此帖子的其他人。 像这样

final NotificationManager notificationManager = (NotificationManager) getSystemService (NOTIFICATION_SERVICE);

final Notification notification = new Notification(R.drawable.icon,"A New Message!",System.currentTimeMillis());

notification.defaults=Notification.FLAG_ONLY_ALERT_ONCE+Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, AndroidNotifications.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);

notification.setLatestEventInfo(AndroidNotifications.this, title,message, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);

然后取消它,你打电话

notificationManager.cancel(NOTIFICATION_ID);

或者你可以打电话

notificationManager.cancelAll();
notificationManager.cancelAll();

您可以cancel()你自己的Notifications通过NotificationManager 您可以决定何时cancel()和何时显示它。

private void cancelNotificationInBar() {
        NotificationCreator notification = new NotificationCreator(getApplicationContext());
        notification.mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        if (notification.mNotificationManager != null) {
            notification.mNotificationManager.cancelAll();
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM