Notificación de Android para intentar borrarlo

He leído muchos ejemplos de cómo crear mensajes de notificación. Lo que quería lograr, es porque la notificación será ejecutada por un widget, me gustaría que la intención de notificación cuando se hace clic para borrar a sí mismo cuando el usuario hace clic en it.I no tienen una actividad para volver a. La notificación para mis propósitos simplemente notificará, nada más. Entonces, ¿cuál sería el código de una intención que acaba de borrarse / cancelarse. El código de abajo es una actividad lanzada por un botón (código del botón no incluido), la notificación será encendida por un servicio de fondo.

CharSequence title = "Hello"; CharSequence message = "Hello, Android!"; final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); final Notification notification = new Notification(R.drawable.icon,"A New Message!",System.currentTimeMillis()); notification.defaults=Notification.FLAG_ONLY_ALERT_ONCE+Notification.FLAG_AUTO_CANCEL; Intent notificationIntent = new Intent(this, AndroidNotifications.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0); notification.setLatestEventInfo(AndroidNotifications.this, title,message, pendingIntent); notificationManager.notify(NOTIFICATION_ID, notification); 

Gracias

Echa un vistazo a FLAG_AUTO_CANCEL

Bit a ser dirigido a bit a bit en el campo de indicadores que debe establecerse si la notificación debe ser cancelada cuando el usuario hace clic en él.

EDIT:

 notification.flags |= Notification.FLAG_AUTO_CANCEL; 

Establezca los indicadores en notification.flags en lugar de notification.defaults.

Ejemplo:

 notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL; 

Si está utilizando NotificationCompat.Builder (una parte de android.support.v4 ), simplemente llame al método de su objeto setAutoCancel

 NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setAutoCancel(true); 

Algunos chicos estaban reportando que setAutoCancel() no funcionó para ellos, así que puedes probar de esta manera también

 builder.build().flags |= Notification.FLAG_AUTO_CANCEL; 

La única manera que puedo ver de hacer esto es hacer que el punto de Intent su Notification apunte a un Service fondo. Cuando se inicia este servicio, se borra la Notification dada utilizando NotificationManager.cancel(int id) . El Service se detendría entonces. No es bonito, y no sería fácil de implementar, pero no puedo encontrar ninguna otra forma de hacerlo.

  /** Post a notification to be shown in the status bar. Obs.: You must save this values somewhere or even pass it as an extra through Intent to use it later */ notificationManager.notify(NOTIFICATION_ID, notification); /** Cancel a previously shown notification given the notification id you've saved before */ notificationmanager.cancel(NOTIFICATION_ID); 

El uso de setContentIntent debería resolver su problema:

 .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0)); 

Por ejemplo:

 NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("title") .setAutoCancel(true) .setContentText("content") .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0)); NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, mBuilder.build()); 

A menudo, es posible que desee dirigir al usuario al contenido relevante y, por lo tanto, puede reemplazar 'new Intent ()' por otra cosa.

  • Android: no se pueden mostrar varias líneas de texto en la notificación
  • ¿Cómo realizar una acción de notificación (clic) en la pantalla de bloqueo?
  • ¿Cómo mostrar las notificaciones de Android en la pantalla, así como el icono de la barra de estado?
  • ¿Por qué los iconos establecidos con Notification.Builder.setSmallIcon en Android Lollipop muestran como un cuadrado blanco?
  • Cómo realizar un seguimiento de las notificaciones para saber cuándo mostrar una notificación resumida
  • Cambio de los botones de acción en una notificación
  • Uri a la notificación de sonido por defecto?
  • ¿Cómo mostrar iconos diferentes para la barra de estado y la barra de notificación en Android?
  • ¿Cómo agregar una insignia de notificación / cuenta al icono de la aplicación en los dispositivos Sony Xperia?
  • PendingIntent obtener requestCode
  • Evitar que el usuario rechace la notificación
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.