Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a combination of Vibration, Lights or Sound for a notification in android

I wan to provide the user with the option of choosing Lights, Sounds or Vibration or a combination of these three for alerts on Notification.

In android docs I saw that there is an option of DEFAULT_ALL where in all the three methods of alerts will be used.

Else there is an option choosing any one of them (DEFAULT_LIGHTS, DEFAULT_VIBRATE, DEFAULT_SOUND).

Is there any way by which a combination of for example SOUND and VIBRATION but no LIGHTS and other combinations can be made?


EDIT

Notification.Builder's (from prolink007's answer) method setDefaults(int default) says that:

The value should be one or more of the following fields combined with bitwise-or: DEFAULT_SOUND, DEFAULT_VIBRATE, DEFAULT_LIGHTS.

How should this be used?

like image 796
Ashwin Avatar asked Aug 17 '12 13:08

Ashwin


3 Answers

The Notification.Builder API 11 or NotificationCompat.Builder API 1 offers a few different methods for setting these types of alerting.

  • setLights(...)
  • setSound(...)
  • setVibrate(...)

The value should be one or more of the following fields combined with bitwise-or: DEFAULT_SOUND, DEFAULT_VIBRATE, DEFAULT_LIGHTS.

Not tested, but i believe you would do something like this if you wanted SOUND, VIBRATION and LIGHTS:

setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE | DEFAULT_LIGHTS);
like image 129
prolink007 Avatar answered Feb 16 '23 01:02

prolink007


For portability I prefer NotificationCompat.
User will possibly prefer his/her default values. In NotificationCombat you can set vibration, light and sound to user's default settings like this:

.setDefaults(-1)

where "-1" matches to DEFAULT_ALL: http://developer.android.com/reference/android/app/Notification.html#DEFAULT_ALL

Not that you must request for VIBRATE permission, else you will get an error. Add this to your Android manifest file:

<uses-permission android:name="android.permission.VIBRATE" />
like image 41
trante Avatar answered Feb 16 '23 01:02

trante


I know this answer has been answered a while ago, but just wanted to offer my way of going about it, which works ideally for a multiple combination of lights, vibration and sound, useful in case you are offering options to the user to enable or disable them.

int defaults = 0;
if (lights) {
    defaults = defaults | Notification.DEFAULT_LIGHTS;
}               
if (sound) {
    defaults = defaults | Notification.DEFAULT_SOUND;
}
if (vibrate) {
    defaults = defaults | Notification.DEFAULT_VIBRATE;
}
builder.setDefaults(defaults);
like image 45
Raveesh Bhalla Avatar answered Feb 16 '23 00:02

Raveesh Bhalla