2010-04-13 29 views
112

Si tengo algunos problemas con una notificación que quiero mostrar en la barra de notificaciones. Aunque configuré el indicador de notificación en Notification.DEFAULT_LIGHTS & Notification.FLAG_AUTO_CANCEL, la notificación no desaparece después de hacer clic en ella. ¿Alguna idea de lo que estoy haciendo mal?La notificación de Android no desaparece después de hacer clic en la notificación

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    int icon = R.drawable.icon; 
    CharSequence tickerText = "Ticker Text"; 
    long time = System.currentTimeMillis(); 

    Notification notification = new Notification(icon, tickerText, time); 
    notification.flags = Notification.DEFAULT_LIGHTS & Notification.FLAG_AUTO_CANCEL; 

    Context context = getApplicationContext(); 
    CharSequence contentTitle = "Title"; 
    CharSequence contentText = "Text"; 
    Intent notificationIntent = new Intent(this, SilentFlipConfiguration.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
    mNotificationManager.notify(1,notification); 

Respuesta

247

Mientras construye Notification por NotificationBuilder puede usar notificationBuilder.setAutoCancel(true);.

+0

Muchas gracias. A mí me funcionó – Sakthimuthiah

+2

Entonces, ¿qué diferencias crear notificaciones utilizando Notificación 'mNotificationManager.notify (1, notificación);' y utilizando NotificationBuilder 'mNotificationManager.notify (1, mBuilder.build());'? Gracias. – NPE

+9

Esta respuesta debe aceptarse, está más en línea con la doctrina actual de diseño de Android – jmaculate

126
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL 

De la documentación:

poco que bitwise- o ed en el campo de los indicadores que deben establece si la notificación debe ser cancelada cuando se hace clic en el usuario

+0

¡Oh hombre, gracias! La próxima vez leeré la documentación de manera más adecuada. – Flo

+3

Esta no es la respuesta correcta. 'Notification.DEFAULT_LIGHTS' es parte de la clase' Notification.defaults', no de la clase 'Notification.flags'. Ver mi respuesta para los instaladores apropiados. – Darcy

+0

Gracias amigo, me ayudó –

27
// Uses the default lighting scheme 
notification.defaults |= Notification.DEFAULT_LIGHTS; 

// Will show lights and make the notification disappear when the presses it 
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS; 
+1

He revisado los documentos de Android. No entiendo muy bien cuándo se deben usar banderas. ¿Por qué no es solo notification.defaults = notification.DEFAULT_LIGHTS suficiente para mostrar las luces. Porque la vibración y el sonido funcionan sin la bandera. – Ashwin

+0

estoy usando el NotificationBuilder, NotificationCompat.Builder mBuilder = nueva NotificationCompat.Builder (este) .setSmallIcon (android.R.drawable.ic_popup_sync) .setContentTitle ("Nueva pío") .setContentText ("Hay" + recuento + "tweets"); mBuilder.setDefaults (NotificationCompat.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL); – Joseph

0

usar la bandera Notification.FLAG_AUTO_CANCEL

Notification notification = new Notification(icon, tickerText, when); 

notification.setLatestEventInfo (contexto, contentTitle, contentText, pendingIntent);

// Cancele la notificación después de su selección notification.flags | = Notification.FLAG_AUTO_CANCEL; y para iniciar la aplicación:

NotificationManager notificationManager = (NotificationManager) context.getSystemService (Context.NOTIFICATION_SERVICE);

Intent intent = new Intent(context, App.class); 

PendingIntent pendingIntent = PendingIntent.getActivity (contexto, intent_id, la intención, PendingIntent.FLAG_UPDATE_CURRENT);

Cuestiones relacionadas