Cómo utilizar Notification.deleteIntent

Estoy tratando de detectar cuando se borra mi notificación. Mi pregunta se refiere directamente a esta respuesta que describe lo que se supone que debo hacer. Así es como estoy implementando las acciones:

// usual Notification initialization here notification.deleteIntent = PendingIntent.getService(context, 0, new Intent(context, CleanUpIntent.class), 0); notificationManager.notify(123, notification) 

Esta es la clase CleanUpIntent:

 class CleanUpIntent extends IntentService { public CleanUpIntent() { super("CleanUpIntent"); } @Override protected void onHandleIntent(Intent arg0) { // clean up code } } 

Después, simplemente lanzar la notificación como lo haría normalmente, pero cuando voy a probarlo (presionando "Borrar todas las notificaciones") no pasa nada. He insertado una línea de código que imprimir algo a LogCat cuando IntentService se inicia, pero nada nunca se ejecutó. ¿Es esto como debo usar Notification.deleteIntent?

Lo que debe hacer es registrar un BroadcastReceiver (probablemente en su AndroidManifest.xml o, alternativamente, utilizando registerReceiver en un Service ) y, a continuación, defina deleteIntent como un Intent que será capturado por ese receptor.

Código de ejemplo que se llamará cada vez que el usuario borre la notificación, espero que le ayude.

  .... notificationBuilder.setDeleteIntent(getDeleteIntent()); .... protected PendingIntent getDeleteIntent() { Intent intent = new Intent(mContext, NotificationBroadcastReceiver.class); intent.setAction("notification_cancelled"); return PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); } 

NotificationBroadcastReceiver.java

 @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(action.equals("notification_cancelled")) { // your code } } 

AndroidManifiest.xml

  <receiver android:name=".NotificationBroadcastReceiver"> <intent-filter> <action android:name="notification_cancelled"/> </intent-filter> </receiver> 

Debería usar getBroadcast methode en lugar de getService y debería registrar receptor para Acción específica.

No se requiere un receptor explícito. DeleteIntent se llamará automáticamente mientras se pulsa el botón de borrado .

  • RemoteView no muestra botones en la notificación personalizada
  • Cómo mostrar varias notificaciones en android
  • Android: guarda el sonido como tono de llamada y notificación
  • Evitar la cancelación de la notificación al eliminar la aplicación de la lista de aplicaciones recientes
  • ¿Es una práctica común preguntar a los usuarios si desean recibir notificaciones push?
  • Android - administrador de notificaciones, tener una notificación sin intención
  • Biblioteca cliente C # para suscribir / publicar MQTT (Realmente Small Message Broker)
  • ¿Cómo las notificaciones necesitan WAKE_LOCK en android?
  • NotificationManager getActiveNotifications () para dispositivos antiguos
  • Notificación de Android en el momento
  • Firebase Cloud Messaging (FCM): inicia la actividad cuando el usuario hace clic en la notificación con extras
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.