Play sound in Android notifications even though the phone is in silent mode

My application displays notifications, and when a notification is displayed, sound is played. But when my phone is in β€œsilent mode”, the notification does not play. I want to "override" the volume settings and play the sound, even though the silent mode is set. Is there a way to do this?

+6
source share
3 answers

Hi, you can use MediaPlayer as the notification sound by running a Service that plays MediaPlayer when the notification is displayed.

To stop the sound, stop the service in the BroadCastReceiver of the PendingIntent notification.

Also, be sure to stop the sound when the notification is canceled.

+5
source
AudioManager am; am= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE); am.setRingerMode(AudioManager.RINGER_MODE_NORMAL); 
+4
source

use this to turn on the notification sound -

 Settings.System.putInt(getContentResolver(),android.provider.Settings.System.NOTIFICATION_SOUND, 1); 

Remember to give permission in the manifest -

 <uses-permission android:name="android.permission.WRITE_SETTINGS"/> 
+1
source

Source: https://habr.com/ru/post/945477/


All Articles