1

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?

mikedal
  • 13
  • 2

1 Answers1

0

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" />
Sadegh.t
  • 205
  • 3
  • 10
  • Nice Job! Thanks. My problem now is to make my notification ring when silence mode is activated on the android device. But I will ask this in another thread. I mark this one [Solved] – mikedal Aug 02 '21 at 17:07
  • In order to play sound in full intent, you should use MediaPlayer in the alarm activity. Because in some devices the notification sound does not work in full-screen mode! – Sadegh.t Aug 02 '21 at 20:02