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?
The Notification.Builder API 11 or NotificationCompat.Builder API 1 offers a few different methods for setting these types of alerting.
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);
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" />
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With