4

Implementing Critical Alerts on Android

 2 weeks ago
source link: https://medium.com/@surendar1006/implementing-critical-alerts-on-android-aa49b4d75705
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Implementing Critical Alerts on Android

Critical alerts are notifications that bypass the mute switch and Do Not Disturb settings on a device, ensuring that the user receives important notifications regardless of their device’s sound settings. While Apple introduced this feature around 2018, Android has not fully integrated it yet. In this article, we’ll delve into how to implement similar critical alerts on Android devices.

Critical alert notification

Source: https://global.discourse-cdn.com/business4/uploads/athom/original/3X/d/9/d9dd62098458b7c38a82f36f52aa0af0266e7c28.jpeg

Requesting DND Override Permission:

To enable critical alerts on Android, we need to override the Do Not Disturb (DND) mode and ringer modes of the device. This requires the DND override permission from the user. Unlike runtime permissions, this permission (ACCESS_NOTIFICATION_POLICY) must be declared in the app manifest and accessed through system settings.

To request DND override permission, we use the following code snippet:

fun openDNDPermissionSettings() {
val intent = Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS)
ActivityCompat.startActivity(this, intent, Bundle())
}

This code opens the ‘Do not disturb override permission’ screen, where users can grant this Do not disturb permission to specific apps.

System settings screen which shows list of apps that requested Do not distrurb permission.

Do not disturb permission screen

Overriding DND Mode for Notifications:

Once we have the DND override permission, we can override the DND mode specifically for critical alert notifications by setting notificationChannel.setBypassDnd(true) . This won’t modify the device's DND mode settings, it just bypasses the DND mode for notifications posted via this channel only.

Overriding Silent/Vibrate Mode:

To ensure that notification sound plays for critical alerts, before triggering
notificationManager.notify check the device’s ringer mode using audioManager.ringerMode. If necessary, set the ringer mode to normal, adjust the volume, and then revert to the original settings after playing the notification sound.

val audioManager = getSystemService(ContextWrapper.AUDIO_SERVICE) as AudioManager
var originalRingMode = audioManager.ringerMode

Remember that in a few devices when DND mode is enabled, we get ringer mode as Silent even though it is normal. So check for notification volume as well, if the volume is greater than 0 then we can assume the original ringer mode is not silent.

Conclusion:

Implementing critical alerts on Android involves obtaining DND override permission, setting notification channels to bypass DND, and managing device settings for sound and volume control. While this feature may have limitations on managed profiles, users on personal profiles can grant DND override permissions to ensure critical alerts function as intended.

For the complete code and project details, please visit the GitHub repository: Critical-Alerts-Android-Demo

Thank you for reading!


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK