He creado una aplicación que crea notificaciones, usando el siguiente código:¿Cómo puedo activar la vibración y las luces con la aplicación de notificaciones de Android?
// notification
Notification notification = new Notification(R.drawable.notification_icon, title, System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// parameters
String ringtone = prefs.getString(context.getString(R.string.key_notifications_ringtone), "");
if (ringtone.length() > 0) {
notification.sound = Uri.parse(ringtone);
notification.audioStreamType = AudioManager.STREAM_NOTIFICATION;
}
boolean useVibrator = prefs.getBoolean(context.getString(R.string.key_notifications_use_vibrator), false);
if (useVibrator) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
boolean useLed = prefs.getBoolean(context.getString(R.string.key_notifications_use_led), false);
if (useLed) {
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
}
// alert
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
contentView.setImageViewResource(R.id.notification_icon, R.drawable.icon);
contentView.setTextViewText(R.id.notification_title, title);
contentView.setTextViewText(R.id.notification_text, text);
notification.contentView = contentView;
Intent notificationIntent = new Intent(context, MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notificationManager.notify(1, notification);
funciona la notificación, y se utiliza el tono correcto.
Sin embargo, a pesar de que las preferencias están activadas correctamente y las banderas de notificación están configuradas correctamente (comprobé por depuración), la notificación nunca vibra y nunca hace que las luces se activen.
Hubiera culpado a la configuración de mi teléfono, pero todas las demás aplicaciones que usan notificaciones, como mensajería, gmail y otras utilizan correctamente todas estas características.
¿Alguien puede saber lo que hice mal? (Mi teléfono es un héroe de HTC con Android 2.1)
¿Tiene el permiso de Vibrar? – Sephy
Probé tu código en mi Nexus One, con el permiso y obtengo la vibración pero no el led ... aún cavando – Sephy
lo mismo, había perdido el permiso de vibración, y ahora esa parte está funcionando. – SirDarius