show notifications - Do Not Disturb mode enabled

497 Views Asked by At

Does someone know how to receive notifications on a device when silent mode is enabled? I'm sending notifications push with a web server and firebase, and it seems impossible to receive a notification when the android device is in silence mode (Do Not Disturb). But, when I put my smartphone in silent mode my alarms are still ringing. Then we should be able to receive a notification categorized "alarm", even in Silence mod. No?

1

There are 1 best solutions below

2
On BEST ANSWER

The best way to show notification in silent mode is to show full-screen intent

 val summaryNotification =
        NotificationCompat.Builder(
            context,
            notificationChannelId
        )
            .setSmallIcon(getSmallIconForNotification())
            .setLargeIcon(largeIcon(context))
            .setStyle(bigPicStyle)
            .setContentTitle(title)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_ALARM)
            .addAction(
                R.drawable.ic_cancel_small,
                context.getString(R.string.dismiss),
                dismissIntent
            )
            .setAutoCancel(true)
            .setTimeoutAfter(durationInMilliSeconds!!.toLong())
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentIntent(
                contentIntent(
                    context,
                    SUMMARY_NOTIFICATION_ID
                )
            )
    if (!showFull) {
        summaryNotification.setSound(sound, AudioManager.STREAM_ALARM)
    }

    if (showFull) {
        summaryNotification.setFullScreenIntent(buildPendingIntent(context, i), true)
    }

    notificationManager.notify(
        SUMMARY_NOTIFICATION_ID,
        summaryNotification.build()
    )

build pending intent:

  private fun buildPendingIntent(
    context: Context,
    intent: Intent
): PendingIntent? {
    return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}

when you receive data from firebase check if the phone is active or not and then send proper notification

 val mPowerManager = context!!.getSystemService(Context.POWER_SERVICE) as PowerManager
            if (!mPowerManager.isInteractive) {
                showFullNotification(true)
            }else{
                showFullNotification(false)
            }

then you have to create an alarming activity.

to hide action bar in alarm activity:

 (this as AppCompatActivity?)!!.supportActionBar!!.hide()

and in the manifests

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