Comment supprimer la notification de la barre de notification par programme dans Android?

Tout le monde a une idée de la façon dont nous pouvons supprimer les notifications de l’application par programmation, ce qui s’appelle utiliser l’intention en attente.

J’ai utilisé pour annuler la notification en utilisant la méthode suivante.

AlarmManager am=(AlarmManager)getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(Display.this, TwoAlarmService.class); PendingIntent pi = PendingIntent.getBroadcast(Display.this, AlarmNumber, intent, PendingIntent.FLAG_CANCEL_CURRENT); am.cancel(pi); 

Mais le problème est la notification qui a déjà été déclenchée et qui n’est pas supprimée de la barre de notification.

Merci d’avance…

entrer la description de l'image ici

Peut-être essayer ceci:

 NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancel(NOTIFICATION_ID); 

OU, vous pouvez également le faire pour annuler toutes les notifications dans un contexte donné:

 notificationManager.cancelAll(); 

Voir ce lien vers la documentation: NotificationManager

Les notifications restnt visibles jusqu’à ce que l’un des événements suivants se produise:

L’utilisateur rejette la notification soit individuellement, soit en utilisant “Effacer tout” (si la notification peut être effacée). L’utilisateur clique sur la notification et vous avez appelé setAutoCancel () lorsque vous avez créé la notification. Vous appelez cancel () pour un ID de notification spécifique. Cette méthode supprime également les notifications en cours. Vous appelez cancelAll (), qui supprime toutes les notifications que vous avez précédemment émises. Si vous définissez un délai d’attente lors de la création d’une notification à l’aide de setTimeoutAfter (), le système annule la notification une fois la durée spécifiée écasting. Si nécessaire, vous pouvez annuler une notification avant l’expiration du délai d’expiration spécifié public void cancelNotification () {

  Ssortingng ns = NOTIFICATION_SERVICE; NotificationManager nMgr = (NotificationManager) getActivity().getApplicationContext().getSystemService(ns); nMgr.cancel(NOTIFICATION_ID); } 

Toutes les notifications (même les autres notifications d’applications) peuvent être supprimées en écoutant «NotificationListenerService», comme indiqué dans la section NotificationListenerService Implementation.

Dans le service, vous devez appeler cancelAllNotifications() .

Le service doit être activé pour votre application via “Applications et notifications” -> “Accès spécial aux applications” -> “Notifications”.

Ajouter au manifeste:

             

Puis en code;

 public class NotificationListenerEx extends NotificationListenerService { public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { NotificationListenerEx.this.cancelAllNotifications(); } }; @Override public void onNotificationPosted(StatusBarNotification sbn) { super.onNotificationPosted(sbn); } @Override public void onNotificationRemoved(StatusBarNotification sbn) { super.onNotificationRemoved(sbn); } @Override public IBinder onBind(Intent intent) { return super.onBind(intent); } @Override public void onDestroy() { unregisterReceiver(broadcastReceiver); super.onDestroy(); } @Override public void onCreate() { super.onCreate(); registerReceiver(broadcastReceiver, new IntentFilter("com.test.app")); } 

Après cela, utilisez le récepteur de diffusion pour tout déclencher.