Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I enable vibration and lights using the Android notifications api?

I have created an application that creates notifications, using the following code:

// 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);

The notification works, and the correct ringtone is used.

However, even though the preferences are correctly activated and notification flags are correctly set (I checked by debugging), the notification never vibrates and never cause the lights to be activated.

I would have blamed my phone's settings, but every other app using notifications, like messaging, gmail, and others correctly use all these features.

May someone know what I did wrong ? (my phone is a HTC Hero with Android 2.1)

like image 779
SirDarius Avatar asked Jul 21 '10 09:07

SirDarius


People also ask

Which API do you use to show a notification in the notification drawer and in the device's status bar?

Android 8.0, API level 26 Apps with active notifications display a notification "badge" on top of their app icon on the home/launcher screen. Users can now snooze a notification from the drawer.

What are the types of notifications available in Android?

notifications in the system bar. sound notifications. notifications by vibration.


2 Answers

Add permission to your manifest file

<uses-permission android:name="android.permission.VIBRATE"></uses-permission>

EDIT

For Lights try adding them explicitly, the Default light might be configured to be nolight

notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
like image 81
Pentium10 Avatar answered Sep 29 '22 02:09

Pentium10


In addition to Pentium10'S answer:

Put your device to sleep and the lights will go on! ;)

like image 27
OneWorld Avatar answered Sep 29 '22 00:09

OneWorld