Question

I am displaying a Notification in status bar depending up on my condition. Up to this, it is OK.

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.

Was it helpful?

Solution

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.

OTHER TIPS

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();
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();
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top