178 votes

Comment rejeter une notification après avoir cliqué sur une action ?

Depuis le niveau 16 de l'API (Jelly Bean), il est possible d'ajouter des actions à une notification à l'aide de la fonction

builder.addAction(iconId, title, intent);

Mais lorsque j'ajoute une action à une notification et que l'on appuie sur l'action, la notification ne sera pas renvoyée. Lorsque l'on clique sur la notification elle-même, on peut la renvoyer avec

notification.flags = Notification.FLAG_AUTO_CANCEL;

ou

builder.setAutoCancel(true);

Mais évidemment, cela n'a rien à voir avec les actions associées à la notification.

Des conseils ? Ou cela ne fait-il pas encore partie de l'API ? Je n'ai rien trouvé.

9voto

À mon avis, l'utilisation d'un BroadcastReceiver est un moyen plus propre d'annuler une notification :

Dans AndroidManifest.xml :

<receiver 
    android:name=.NotificationCancelReceiver" >
    <intent-filter android:priority="999" >
         <action android:name="com.example.cancel" />
    </intent-filter>
</receiver>

Dans le fichier java :

Intent cancel = new Intent("com.example.cancel");
PendingIntent cancelP = PendingIntent.getBroadcast(context, 0, cancel, PendingIntent.FLAG_CANCEL_CURRENT);

NotificationCompat.Action actions[] = new NotificationCompat.Action[1];

NotificationCancelReceiver

public class NotificationCancelReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //Cancel your ongoing Notification
    };
}

7voto

CommonsWare Points 402670

Vous pouvez toujours cancel() le site Notification de ce qui est invoqué par l'action (par exemple, en onCreate() de l'activité liée à la PendingIntent que vous fournissez à addAction() ).

6voto

Hanisha Points 119

Il suffit de mettre cette ligne :

 builder.setAutoCancel(true);

Et le code complet est :

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(android.R.drawable.ic_dialog_alert);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.co.in/"));
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    builder.setContentIntent(pendingIntent);
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.misti_ic));
    builder.setContentTitle("Notifications Title");
    builder.setContentText("Your notification content here.");
    builder.setSubText("Tap to view the website.");
    Toast.makeText(getApplicationContext(), "The notification has been created!!", Toast.LENGTH_LONG).show();

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    builder.setAutoCancel(true);
    // Will display the notification in the notification bar
    notificationManager.notify(1, builder.build());

2voto

Amit Points 13

Juste pour conclure :

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

Intent intent = new Intent(context, MyNotificationReceiver.class);
intent.putExtra("Notification_ID", 2022);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
            context,
            0,
            intent,
            ...);

Notification notification = new NotificationCompat.Builder(...)
...    
.addAction(0, "Button", pendingIntent)
.build();

notificationManager.notify(2022, notification);

et pour rejeter la notification vous avez deux options :

approche 1 (en MyNotificationReceiver )

NotificationManager manager = (NotificationManager)
            context.getSystemService(NOTIFICATION_SERVICE);
    manager.cancel(intent.getIntExtra("Notification_ID", -1));

approche 2 (en MyNotificationReceiver )

NotificationManagerCompat manager = NotificationManagerCompat.from(context);
manager.cancel(intent.getIntExtra("Notification_ID", -1));

et enfin dans manifest :

<receiver android:name=".MyNotificationReceiver" />

-3voto

rudakovsky Points 36

Builder.setAutoCancel(true) ;

Testé sur Android 9 également.

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X