How to Show an Activity on Lock Screen instead of a Notification
Tags: technology, android, kotlin
Published: 2021-07-01 11:37:22

---
title: How to Show an Activity on Lock Screen instead of a Notification
published: true
date: 2021-06-10 23:10:25 UTC
tags: Technology,android,kotlin
canonical_url: https://victorbrandalise.com/how-to-show-activity-on-lock-screen-instead-of-notification/
---

Today we’re gonna learn how to show a full screen activity instead of a notification when the device is locked.

<iframe loading="lazy" title="Full Screen Activity Notification" width="1200" height="675" src="https://www.youtube.com/embed/gcCOUM1tOx4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

By default Android will show the same notification it shows when the device is unlocked but in some cases, such as a phone call, you might want to display a full screen activity.

You can find the [source code here](https://github.com/victorbrndls/BlogProjects/tree/lockscreen-notification).

## Creating The Notification

Let’s start by creating a simple notification that’ll be used later to show an Activity on the look screen.

The first thing we need to do before creating the notification is creating the channel to display it.

<script src="https://gist.github.com/victorbrndls/96b02353e8bf827bff0188c30087f5ac.js"></script>

First we retrieve the `NotificationManager` from `NotificationManagerCompat`. That’s the interface used for dealing with notifications on Android. Then we create a `NotificationChannel` that takes the channel’s id, name and importance as arguments. It’s advised to use `NotificationManager.IMPORTANCE_HIGH` for increasing the chances of the notification appearing as a heads up notification. We also change the `lockscreenVisibility` to `Notification.VISIBILITY_PUBLIC` to tell Android that the notification can be shown on the lock screen. Finally we call `notificationManager.createNotificationChannel` to register the channel.

Up until Android Nought (25), there’s no need to create notification channels, that’s why there’s a guard cause at the beginning of the method.

### When Should I Create the Channel?

There’s no problem in calling `createNotificationChannel` multiple times with the same channel, Android will ignore it if there’s an existing channel with the same id.

You should register your channels as early as possible, preferably when `Application.onCreate` is called.

**Channels are immutable** , if you need to change some channel configuration you’ll either have to delete the app or change the channel’s id.

### Creating a Simple Notification

Now that we have created a channel, we can proceed to creating the notification.

<script src="https://gist.github.com/victorbrndls/686fa3509c794b31871826e1f06cb756.js"></script>

First we start by creating a `PendingIntent`, that’s the intent that will be called when the notification is clicked, here we’ll simply start an activity. Then we call `NotificationCompat.Builder` to define how the notification will look like. The `CHANNEL_ID` parameter has to have the same id we used earlier to create the channel. If you want to learn how to customize you notification you can take a look at the [Android Documentation](https://developer.android.com/training/notify-user/build-notification).

The next step is simply calling `notificationManager` to show the notification. The notification id can be any number, it’s used only if you need to interact with the notification later.

<script src="https://gist.github.com/victorbrndls/799b51f0d53d0c9e9ea8736b21a6ec2d.js"></script>

That’s what you should see when your notification is displayed.

![](https://victorbrandalise.com/wp-content/uploads/2021/06/Screenshot-2021-06-08-at-20-42-18-Photo-Google-Photos.png)<figcaption>Heads Up Notification</figcaption>

## Creating the Lock Screen Activity

Now we’ll create the Activity that’ll be displayed on the lock screen. That activity is very similar to a normal activity but there are 2 things you need to change for it to appear on the lock screen.

The first thing is calling `showWhenLockedAndTurnScreenOn` after `onCreate` to define that the activity can appear on the lock screen and that the screen should be turned on when it appears. I added the method to the activity but you can easily add that as an extension to Activity in case you use it in other places.

<script src="https://gist.github.com/victorbrndls/390bdb16d814f0d3c272322bfc291a62.js"></script>

The second thing is changing the manifest definition by adding `launchMode` and `showOnLockScreen`.

<script src="https://gist.github.com/victorbrndls/617507dcb0e76565e286cceb49a6e542.js"></script>

That’s it, you’ve created a activity that can appear on the lock screen. The last thing we need to do is to tell Android that we want that activity to appear on the lock screen instead of the notification.

### Adding the Activity to Notification

We’ll have to modify the `createNotification` method we defined earlier. We need to create another `PendingIntent` that’ll start the activity that’ll be shown on the lock screen. We then pass that intent to `setFullScreenIntent`. It’s also important to add a category such as ALARM or CALL for increasing the chances of it appearing on the lock screen.

<script src="https://gist.github.com/victorbrndls/8dd56dd41c7de1306bfc29fe185937d6.js"></script>

Here’s the end result

<iframe loading="lazy" title="Full Screen Activity Notification" width="1200" height="675" src="https://www.youtube.com/embed/gcCOUM1tOx4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

<figcaption>Full Screen Activity on Lock Screen</figcaption>
* * *

### It’s not Working !!!

For some of you the activity might not appear on the lock screen and that’s because of your phone, not all phones allow full screen intents by default. I have a _Xiaomi_ and it’s not enabled by default.

To enable it you have to go to the **app configuration** -> **Other permissions** -> **Show On Lock screen**.

![](https://victorbrandalise.com/wp-content/uploads/2021/06/1623282460476-COLLAGE-1024x768.jpg)<figcaption>Enable App to Appear on Lock Screen</figcaption>

The notification will also not appear if you have an existing notification with the same id that has not been dismissed. Ideally you should cancel the notification by calling `notificationManager.cancel(id)` when the lock screen activity is destroyed.

## Conclusion

Android notifications are a great way to notify your users they need to do something but they might not be work well when the phone is locked. Full screen activities come to solve that problem, allowing you to show the activity even if the phone is locked.

The source code can be found [here](https://github.com/victorbrndls/BlogProjects/tree/lockscreen-notification).

## Resources

[How to manage incoming video call for every Android OS version with FCM notifications](https://medium.com/simform-engineering/how-to-manage-incoming-video-call-for-every-android-os-version-with-fcm-notifications-68b8f2e3c9eb)

[Show an urgent message](https://developer.android.com/training/notify-user/build-notification#urgent-message)

Photo by [Rami Al-zayat](https://unsplash.com/@rami_alzayat?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/phone-screen?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)

The post [How to Show an Activity on Lock Screen instead of a Notification](https://victorbrandalise.com/how-to-show-activity-on-lock-screen-instead-of-notification/) first appeared on [Victor Brandalise](https://victorbrandalise.com).