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
- TaskStackBuilder.addParentStack no funciona cuando estaba construyendo una notificación
- BroadcastReceiver no funciona cuando la aplicación no se está ejecutando
- Firebase (FCM): activa la actividad y pasa los datos al hacer clic en la notificación. androide
- FCM push notification issue: - "error": "NotRegistered"
- Los botones de notificación de Android no aparecen
- Notificación con sonido aunque el teléfono esté enmudecido
- Android La intención pendiente iniciada desde notificaion no sustituye a la última
- Mostrar siempre servicio en la barra de notificación
- Android: actualización de la barra de progreso de la notificación, correctamente
- Notificación setLights () Valor predeterminado?
- Apertura del navegador en la notificación push
- Android: ejecutando una tarea de fondo con AlarmManager
- Cancelar notificación dinámica en Android cuando se selecciona la notificación
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.
- ¿Hay efectos secundarios del uso de múltiples archivos string.xml?
- ¿Cómo puedo comprobar si una aplicación es una aplicación que no es de sistema en Android?