2012-05-23 28 views
9

Tengo un código que crea algunas notificaciones, es realmente básico.Android 4: no se puede ignorar la notificación al deslizar

int icon = R.drawable.notification; 
CharSequence tickerText = "Text"; 
long when = System.currentTimeMillis(); 
Notification notification = new Notification(icon, tickerText, when); 

Context context = getApplicationContext(); 
CharSequence contentTitle = "Text"; 
CharSequence contentText = "Text"; 
Intent notificationIntent = new Intent(this, RequestActivity.class); 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
notification.flags |= Notification.DEFAULT_SOUND; 
notification.flags |= Notification.DEFAULT_VIBRATE; 
notification.flags |= Notification.DEFAULT_LIGHTS; 
notification.flags |= Notification.FLAG_AUTO_CANCEL; 

mNotificationManager.notify(notificationID, notification); 

Todo funciona bien en 2.1. En 4.0, todo funciona bien, excepto que la acción de deslizar para descartar no funciona. La notificación va levemente hacia un lado y luego se pega y rebota. ¿Alguna idea? Gracias.

+0

intenta quitar FLAG_AUTO_CANCEL –

+0

@Copa ya se intentó – James

+0

mh ... tratar de asignar la primera bandera a ntofication.flags y no a modo bit o la primera bandera. Cambie notification.flags | = DEFAULT_SOUND a notification.flags = Notification.DEFAULT_SOUND; Tal vez esto ayude –

Respuesta

12

No puede deslizar su notificación porque está en estado "EN CURSO".

En primer lugar la solución:

Reemplazar el establecimiento de banderas con el siguiente código:

notification.defaults |= Notification.DEFAULT_SOUND; 
notification.defaults |= Notification.DEFAULT_VIBRATE; 
notification.defaults |= Notification.DEFAULT_LIGHTS; 
notification.flags |= Notification.FLAG_AUTO_CANCEL; 

predeterminados son para la sección por defecto, banderas de la sección banderas.

¿Y ahora la razón por la que estaba en curso?

Como ya sabrá, los indicadores (y valores predeterminados) para las notificaciones se establecen mediante un bitwise operation. Significa que cada indicador tiene un valor constante que es una potencia de 2. Al agregarlos se obtiene un número único para un conjunto de indicadores que hace que sea realmente rápido calcular qué indicadores están realmente establecidos.

Notification.DEFAULT_VIBRATE y Notification.FLAG_ONGOING_EVENT tienen el mismo valor contant de .

+0

Esto era exactamente lo que necesitaba saber. ¡Gracias! – joshplusa

+0

Empecé a sonar con setOngoing (falso) y notification.flags | = Notification.FLAG_AUTO_CANCEL. –

0

basta con insertar esta línea cuando se hace la notificación ...

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

que puedes usar setOngoing (boolean ongoing)

Permite configurar si se trata de una notificación "en curso". Las notificaciones continuas no pueden ser descartadas por el usuario, por lo que su aplicación o servicio debe tenga cuidado de cancelarlos. Normalmente se utilizan para indicar una tarea de fondo con la que el usuario está participando activamente (por ejemplo, reproduciendo música ) o está pendiente de alguna manera y ocupando el dispositivo (por ejemplo, descarga de archivos, operación de sincronización, conexión de red activa) .

Puede utilizar

.setOngoing(false); 
Cuestiones relacionadas