17

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)

SirDarius
  • 41,440
  • 8
  • 86
  • 100

2 Answers2

28

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;
Pentium10
  • 204,586
  • 122
  • 423
  • 502
  • I suggested that as well, but after a bit of thinking,that can't be it. When you don't have a permission, the application crashes doesn't it? so he wouldn't have seen the rest working – Sephy Jul 21 '10 at 10:23
  • that's what I thought, too, a missing permission should have thrown an exception. – SirDarius Jul 21 '10 at 10:31
  • to complete my previous comment, adding the missing permission did the trick, however still no lights, and I didn't find a specific permission for that purpose – SirDarius Jul 21 '10 at 10:38
  • Okay, I'm accepting this answer, because, for some reason, and without changing the code, the lights now are working, even without having to specify a non-default led scheme. – SirDarius Jul 22 '10 at 12:58
  • 2
    I'm using the Nexus 5 and the lights are not showing up. What did you do? Anyone? – Kala J Oct 18 '14 at 01:07
15

In addition to Pentium10'S answer:

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

OneWorld
  • 17,512
  • 21
  • 86
  • 136
  • Hello OneWorld try the above code and put the this code also note.defaults |= Notification.DEFAULT_SOUND; note.defaults |= Notification.DEFAULT_VIBRATE; by this the vibrate and sound works but the light is not work even I put my device in sleep and light will go off. – Yog Guru Oct 17 '11 at 09:26
  • 1
    For anyone who comes across this in the future, make sure the phone is off as OneWorld said. Also make sure the notifications tray has not been pulled down already (i.e. user hasn't looked that the notification). The LED will only flash in this case. – ashishduh Feb 06 '14 at 19:12