Option to disable sound and vibrate from api
Currently we do have these options available for all OneSignal notifications received by the app via the enableVibrate and enableSound SDK methods.
We will look into adding these options to our REST API as well.
@varunnayak Sound can now be disabled on a per notification bases by setting the name to "null" or "nil" as of SDK version 3.2.0.
@jkasten2 We need to set our push message with 'unconditionally vibration off' regardless of user's OS setting(vibrate mode/sound/totally silent). (when user set their alert in-app option) I think currently android SDK reference doesn't describe like this. How can we do that?
@arsens24 You will need to set both enableVibrate and enableSound to false.
If you only set enableVibrate it will still vibrate when the device is set to vibrate only.
@jkasten2 Thanks. You mean, if I set both enableVibrate and enableSound to false, user don't receive any vibration regardless of user's device setting (vibration, sound, totally sirent mode). Is that right?
@arsens24 Correct, the notification will show but it will not vibrate no matter what mode the device is in.
Related to this thread, we are trying to disable vibration for Android on a per notification basis. Disabling sound works, by setting it to "nil", but the phone will still vibrate. On iOS, setting the sound to "nil" disables both sound and vibrate, which is the desired behavior.
Is there any way to do this on Android? To be clear, we want to be able to disable both sound & vibrate for certain notifications, regardless of the user's settings, while sending other notifications with the user's default preference (sound or vibrate).
@ptrwtts On Android sound and vibration are different properties on the notification as posed to iOS where it just has sound. A per notification vibration setting for Android isn't built into our API yet. However we provide a NotificationExtenderService with our SDK so you can customize this this way until we make the is a standard feature.
Ah nice. So if I understand correctly, we would pass some custom data in the push payload, telling the NotificationExtenderService to disable vibrate for this particular notification? Would we then use the OneSignal SDK to disable it, or would we be doing it on the Android level?
@ptrwtts Correct, you will need to add your own additional data to the notification and read it in the service. You will need to change the value on the NotificationCompat.Builder the is provided. Example below.
(exact code not tested)
public class NotificationExtenderExample extends NotificationExtenderService {
@Override
protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
boolean shouldVibrate = true;
JSONObject data = receivedResult.payload.additionalData;
if (data != null && data.has("disableVibrate")
shouldVibrate = false;
final boolean finalShouldVibrate = shouldVibrate;
OverrideSettings overrideSettings = new OverrideSettings();
overrideSettings.extender = new NotificationCompat.Extender() {
@Override
public NotificationCompat.Builder extend(NotificationCompat.Builder builder) {
if (finalShouldVibrate)
return builder;
return builder.setVibrate(null);
}
};
OSNotificationDisplayedResult displayedResult = displayNotification(overrideSettings);
Log.d("OneSignalExample", "Notification displayed with id: " + displayedResult.androidNotificationId);
return true;
}
}
You will need add disableVibrate as a key as additional data to your notification. Note, only the presence of the key is only used in this code.
Awesome, thank you! Will report back when we get a chance to try it.
Hey guys,
I am facing the same issue.
I've tried to apply @jkasten2 code into my app, but I'm not sure if I misplaced it.
I am using Ionic with onesignal plugin, and I've put the code under platforms/android inside OneSingalPush.java.
Can you please help? :)
NVM managed to get the code to be called, but vibration still occurs! :( Any ideas?
NVM solved it :)
Vibration and sound is now settable per Notification Categories (AKA Channel) instead of per notification along with a few other settings part of the release of Android Oreo (8.0).
Make sure you are using the latest 3.6.1 SDK and follow the guide below on using them. https://documentation.onesignal.com/docs/android-notification-categories
Note that even for pre-O devices the Notification Categories settings will apply
I faced the same issue in the 8.1 Made the below change.. Cahnge the prority of the notification as LOW and it will disable the sound of notificatioin
NotificationManager.IMPORTANCE_LOW
the code is like : NotificationChannel chan1 = new NotificationChannel("default", "default", NotificationManager.IMPORTANCE_LOW);