Android silent mode – is there a way for a notification to sound?

Solution for Android silent mode – is there a way for a notification to sound?
is Given Below:

Context

I am working on an app that uses FCM. The use of this application is to alert a user of an event that is occurring (such as an alarm system). In view of the alarm nature of the notification, it is essential that a sound is played when receiving a notification even if the smartphone is in silent or vibrate mode.


Question

Is there a way to achieve this described behavior for all smartphone modes (silent, vibrate, sound) ?


What I’ve tried

  • As I am working with API26> I created a notification channel to have the highest priority which is Max Priority,

  • I’ve set the notification channel to bypass Do Not Disturb mode like so:

    notificationChannel.SetBypassDnd(true);

    Obviously it only affects the Do Not Disturb mode and absolutely not what I want,

  • In the notification builder, I’ve set the notification priority to Max and the category to Alarm:

    .SetPriority(NotificationCompat.PriorityMax)
    .SetCategory(NotificationCompat.CategoryAlarm);
    

    Reading the Android documentation, this feature is also related to Do Not Disturb Mode.

I am actively looking for a solution to this problem, but at this point I’m a bit stuck.


Any suggestions ?

I’ve read about a full screen intent in the Android documentation but it’s not written that a sound will fire if the smartphone is in silent mode.

Maybe there is a way to create a service that rings when the notification arrives? But this service has to be running all the time, which isn’t really a good design idea.

If you guys have any idea, any remarks or suggestions, i’d be grateful to read them !

I believe you need to set priority for your notification.

    private fun setPriorityForAlarmNotification() {
    if (notificationManager.isNotificationPolicyAccessGranted) {
      notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY)
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        val policy = NotificationManager.Policy(PRIORITY_CATEGORY_ALARMS, 0, 0)
        notificationManager.notificationPolicy = policy
      }
    }
  }

As I can see you setCategory for your notification builder is NotificationCompat.CategoryAlarm.

However, in order to set this priority, you need this permission on your manifest

<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" /> 

And request permission if needed

    fun requestNotificationPolicyPermission() {
    val notificationManager = activity!!.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
    
    if (!notificationManager.isNotificationPolicyAccessGranted) {
      val intent = Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS)
      startActivityForResult(intent, REQUEST_CODE_NOTIFICATION_POLICY)
    }
  }

This solution should work absolutely. Hope this can help you 😀