void
void

Reputation: 207

Close android notification when application starts

I have a system notification in my application which will notify the user on receiving a certain event when the application is in background. When I open the app by clicking the notification, app opens fine and dismisses the notification. However, when I open the app using the app icon(either from notification bar or home screen) notification is not dismissed.

Is there a way to dismiss notification (if it is visible in notification bar) on starting the app?

Please find my notification code below:

final int notificationId = 9999;

final NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(context)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentTitle("Notification title")
    .setContentText("Notification text")
    .setAutoCancel(true)
    .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MyActivity.class), 0));
final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notificationId, mBuilder.build());

Upvotes: 0

Views: 687

Answers (1)

Anton Vasilev
Anton Vasilev

Reputation: 104

Just do this:

mNotificationManager.cancel(notificationId);

or cancel all your app's notifications:

mNotificationManager.cancelAll();

Upvotes: 3

Related Questions