Remove the notification icon from the status bar

后端 未结 4 967
说谎
说谎 2020-12-01 15:07

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 alert, that icon will be display

相关标签:
4条回答
  • 2020-12-01 15:22
    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 讨论(0)
  • 2020-12-01 15:23

    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);
    
    0 讨论(0)
  • 2020-12-01 15:26

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

    0 讨论(0)
  • 2020-12-01 15:31

    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());
    
    0 讨论(0)
提交回复
热议问题