My app is showing notifications, and when a notification is shown, a sound is played. But when my phone is on "silent mode" the notification is not played. I want to "override" volume settings, and play a sound although silent mode is set. There is a way to do it?
Asked
Active
Viewed 1.4k times
12
-
5Would you be happy if you set silent mode for you phone when in important meeting and still get notification sound from one app? It is reasone why user sets silent mode. – user123 May 21 '13 at 08:07
-
4Yes, because my app will have an option like this: "play sound although silent mode is set", so user can use this feature or not ;) – Sergio Viudes May 21 '13 at 08:23
-
So only solution is change mode to normal, play notification sound and then set silent mode back. – user123 May 21 '13 at 08:24
-
1There are situations where you still want to override this feature. For example if you want to ping your phone from a bluetooth device to find it. Then you would in 100% of the cases want the phone to sound even if its in silent mode. – nilsi Feb 01 '17 at 05:15
3 Answers
8
Hi you can use MediaPlayer as the notification sound, by starting a Service that play's the MediaPlayer when the notification is shown.
To make the sound stop, stop the service in the BroadCastReceiver of the notification's PendingIntent.
Also, don't forget to stop the sound when the notification cancels.

Abdullah
- 9,005
- 1
- 23
- 24
-
I think your answer can be a good way to do it. I'll try it, thanks! – Sergio Viudes May 27 '13 at 08:38
7
AudioManager am;
am= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

user123
- 1,053
- 15
- 27
-
Thanks for your answer. How can I use it with notifications? Should set RINGER_MODE_NORMAL before call to myNotificationManager.notify and set it to silent again after? It's the correct way to do it? – Sergio Viudes May 21 '13 at 08:26
-
It's not working :( I set mode to normal, then I show notification, and then I set it back to silent again, but there is no sound... If I not set it back to silent, sound plays... I whish that NotificationManager.notify had a callback to execute code when notification is shown :( – Sergio Viudes May 21 '13 at 10:06
-3
use this to enable notification sound -
Settings.System.putInt(getContentResolver(),android.provider.Settings.System.NOTIFICATION_SOUND, 1);
Dont forget give permission in manifest -
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

mihirjoshi
- 12,161
- 7
- 47
- 78
-
What does it? It's the same that this?: AudioManager am; am= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE); am.setRingerMode(AudioManager.RINGER_MODE_NORMAL); – Sergio Viudes May 27 '13 at 08:35
-
It will modify system settings to play sound for your notification.If it wont work for you try different settings like `SOUND_EFFECTS_ENABLED` , `VOLUME_NOTIFICATION`, `VOLUME_SYSTEM`. For detail list refer [this](http://developer.android.com/reference/android/provider/Settings.System.html). – mihirjoshi May 27 '13 at 17:15
-
2-1: Additional permission required and I dont think the user would be happy to have his Settings changed. There are other ways to acheive this that are both less hacky and gives the user a better experience. – zoltish Jan 21 '15 at 07:37