简体   繁体   中英

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?

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

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

On jelly bean devices, led only works if notification priority is set to max or default, please check again. Following snippet of code is working fine for me on jb devices.

notification.setLights(0xFF0000FF,100,3000);
notification.setPriority(Notification.PRIORITY_DEFAULT);

Here I'm showing blue color led for notification which will remain on for 100 ms and off for 3000 ms till user unlocks his device.

And check if you are using NotificationCompat (compatibility) class than ignore setDefaults method and use SetLight, SetSound, Setvibration, etc

I got the same issue, but after landing here got a better solution. Here's a kotlin one:

fun getDefaults(val isSoundOn: Boolean, val isVibrate: Boolean, val isLightOn: Boolean): Int {
    var defaults = 0
    if (isLightOn && isSoundOn && isVibrate)
        return Notification.DEFAULT_ALL
    if (isLightOn) {
        defaults = defaults or Notification.DEFAULT_LIGHTS
    }
    if (isSoundOn) {
        defaults = defaults or Notification.DEFAULT_SOUND
    }
    if (isVibrate) {
        defaults = defaults or Notification.DEFAULT_VIBRATE
    }
    return defaults
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM