开发者

How do I remove Notification from status bar?

开发者 https://www.devze.com 2022-12-30 10:26 出处:网络
I am displaying a Notification in status bar depending up on my condition. Up to this, it is OK.开发者_Go百科

I am displaying a Notification in status bar depending up on my condition. Up to this, it is OK.开发者_Go百科

Now problem in my application is when I come back to application, still the Notification is displayed in the status bar.

I don't want the Notification when I come back from the application. For this give me some suggestions.


I don't have enough rep to add comment so @Commonware . . . it would be more helpful to future readers if you gave more specific details like telling them to use a "notification id" when they create the notification and use that ID to cancel the notification. If you give an answer make it complete or at least provide the full details. These posts are not only meant for the OP it helps others who come across this post as well. like so

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

Then to cancel it you call

notificationManager.cancel(NOTIFICATION_ID);

or you can call

notificationManager.cancelAll();


You can cancel() your own Notifications via the NotificationManager. It is up to you to decide when to cancel() it and when to show it.


notificationManager.cancelAll();


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

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

精彩评论

暂无评论...
验证码 换一张
取 消