Full-Screen Intent Notifications — Android

What are Full-Screen Intents?

Your app might need to display an urgent, time-sensitive message, such as an incoming phone call or a ringing alarm. In these situations, you can associate a full-screen intent with your notification.

Why should Full-Screen Intents be used?

When should Full-Screen Intents be used?

Show me the code

1. Notify while the app on the foreground.

Demonstration of the first scenario, showing full-screen intent notification while on the foreground
val builder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(android.R.drawable.arrow_up_float)
.setContentTitle(title)
.setContentText(description)
.setPriority(NotificationCompat.PRIORITY_HIGH)

// request code and flags not added for demo purposes
val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)

builder.setFullScreenIntent(pendingIntent) // THIS HERE is the full-screen intent
<activity android:name=".LockScreenActivity"
android:showOnLockScreen="true"/>

2. Schedule Full-Screen Intent Notification

Schedule Full-Screen Intent Notification demo
fun Context.scheduleNotification(isLockScreen: Boolean) {
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val timeInMillis = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(SCHEDULE_TIME)

with(alarmManager) {
setExact(AlarmManager.RTC_WAKEUP, timeInMillis, getReceiver(isLockScreen))
}
}

private fun Context.getReceiver(isLockScreen: Boolean): PendingIntent {
// for demo purposes no request code and no flags
return PendingIntent.getBroadcast(
this,
0,
NotificationReceiver.build(this, isLockScreen),
0
)
}
class NotificationReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {
if(intent.getBooleanExtra(LOCK_SCREEN_KEY, true)) {
context.showNotificationWithFullScreenIntent(true)
} else {
context.showNotificationWithFullScreenIntent()
}
}

companion object {
fun build(context: Context, isLockScreen: Boolean): Intent {
return Intent(context, NotificationReceiver::class.java).also {
it.putExtra(LOCK_SCREEN_KEY, isLockScreen)
}
}
}
}

private const val LOCK_SCREEN_KEY = "lockScreenKey"

3. Full-Screen Intent on Lock Screen with a Keyguard

Not requesting keyguard demonstration.
fun Activity.turnScreenOnAndKeyguardOff() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true)
setTurnScreenOn(true)
} else {
window.addFlags(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
or WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
)
}

with(getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
requestDismissKeyguard(this@turnScreenOnAndKeyguardOff, null)
}
}
}
Demonstration with Keyguard On

Conclusion

Android Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store