OneSignal-Android-SDK icon indicating copy to clipboard operation
OneSignal-Android-SDK copied to clipboard

Option to disable sound and vibrate from api

Open varunnayak opened this issue 10 years ago • 16 comments

varunnayak avatar Jan 07 '16 04:01 varunnayak

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.

jkasten2 avatar Jan 19 '16 23:01 jkasten2

@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 avatar Sep 14 '16 09:09 jkasten2

@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 avatar Mar 01 '17 05:03 arsens24

@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 avatar Mar 01 '17 05:03 jkasten2

@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 avatar Mar 01 '17 06:03 arsens24

@arsens24 Correct, the notification will show but it will not vibrate no matter what mode the device is in.

jkasten2 avatar Mar 01 '17 07:03 jkasten2

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 avatar May 03 '17 17:05 ptrwtts

@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.

jkasten2 avatar May 03 '17 18:05 jkasten2

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 avatar May 03 '17 19:05 ptrwtts

@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.

jkasten2 avatar May 03 '17 19:05 jkasten2

Awesome, thank you! Will report back when we get a chance to try it.

ptrwtts avatar May 03 '17 19:05 ptrwtts

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? :)

harizibarak avatar Jul 08 '17 13:07 harizibarak

NVM managed to get the code to be called, but vibration still occurs! :( Any ideas?

harizibarak avatar Jul 08 '17 14:07 harizibarak

NVM solved it :)

harizibarak avatar Jul 08 '17 14:07 harizibarak

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

jkasten2 avatar Aug 22 '17 01:08 jkasten2

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

kuldip4u avatar Jul 25 '18 13:07 kuldip4u