开发者

Remove the notification icon from the status bar

开发者 https://www.devze.com 2022-12-30 22:49 出处:网络
I am displaying an icon in status bar.Now I want to remove that icon immediately when I open that content, after some time if we receive any a开发者_JAVA技巧lert, that icon will be displayed again. Ho

I am displaying an icon in status bar.Now I want to remove that icon immediately when I open that content, after some time if we receive any a开发者_JAVA技巧lert, that icon will be displayed again. How can I do this?


Use the NotificationManager to cancel your notification. You only need to provide your notification id.

https://developer.android.com/reference/android/app/NotificationManager.html

private static final int MY_NOTIFICATION_ID= 1234;
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager;
mNotificationManager = (NotificationManager) getSystemService(ns);
mNotificationManager.notify(MY_NOTIFICATION_ID, notification);

The example code is not complete. It depends on how your created your notification. Just make sure you use the same id to cancel your notification as you used when you created your notification.

To cancel:

mNotificationManager.cancel(MY_NOTIFICATION_ID);


If you want to remove the notification once user clicked on it, set notification flag FLAG_AUTO_CANCEL before you create the notification.


I used the Builder patter so you can just set auto cancel from the setter setAutoCancel(true). That looks something like this:

    String title = "Requests"; 
    String msg = "New requests available.";
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_gcm_icon)
                    .setContentTitle(title)
                    .setAutoCancel(true)
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(msg))
                    .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());


Intent resultIntent = new Intent(application, MainActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(application, 0, resultIntent, 0);
NotificationManager nmgr = (NotificationManager) application.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(application)
            .setSmallIcon(R.drawable.icon_battery)
            .setContentTitle(application.getString(R.string.app_name))
            .setContentText("your text")
            .setOnlyAlertOnce(false)
            .setAutoCancel(true)
            .setTicker("your ticker")
            .setDefaults(Notification.DEFAULT_SOUND  ) //| Notification.DEFAULT_VIBRATE
            .setContentIntent(resultPendingIntent)
            .setVisibility(VISIBILITY_SECRET)
            .setPriority(Notification.PRIORITY_MIN);

Notification mNotification = mBuilder.build();
//  mNotification.flags |= FLAG_NO_CLEAR;
nmgr.notify(0, mNotification);
0

精彩评论

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

关注公众号