How to manage incoming video call for every Android OS version with FCM notifications

Incoming video call banner

While working on the Video calling feature, I was unable to open activity for android OS version 10 via FCM notifications. When I went through the android documentation it said that there are few restrictions on starting activities from background.

https://developer.android.com/guide/components/activities/background-starts

So, How to open activity when the app is killed or the device is locked?

Credit : https://giphy.com/gifs/studiosoriginals-gilphabet-xTiN0IuPQxRqzxodZm

In this article, we’ll walk you through how to manage video call for different android OS version and how to manage firebase push notifications.

When we talk about video call we need to show notification and answer decline screen in below conditions:

  1. When app is in the background
  2. When app is in Foreground
  3. When app is Killed
  4. When device is locked

When the app is killed your firebase service will not be able to handle push notification coming from the backend so in that case, you need to remove the ‘notification’ part from the payload and only send the ‘data’ part into the payload from the backend.

Let’s take a look in the above scenarios:

1. When app is in the background

You can start your activity by simply calling ‘startActivity(intent)’ method in onMessageReceived method. Make sure you have set priority to high for this type of notifications. Simply send a broadcast from firebase service and open activity on receiving broadcast.

Note: Above code works fine in API level < 29 (Android 10), For API level > 29 you can find the solution in below steps.

2. When app is in Foreground

You can start your activity by calling ‘startActivity(intent)’ method in onMessageReceived method. This case will work in every Android OS. You can simply start activity by sending a broadcast from firebase service and open activity on receiving that broadcast.

3. When app is Killed

You need to start your activity using application context as below in onMessageReceived method:

applicationContext.startActivity(intent, options)

Note: Above code will open activity in API level < 29 (Android 10) , for Api level ≥29 you can not start activity if your app is killed. You can show on screen notification with Answer/Reject buttons as below. For showing on screen notification, you need to set full screen intent as described in scenario 4.

incoming call notification

4. When device is locked

You can simply add below line in your activity tag in AndroidManifest.xml:

android:launchMode="singleTop"
android:showOnLockScreen="true"

You also need to set Flags like below in the Java/Kotlin file of your activity:

window.addFlags(
LayoutParams.FLAG_SHOW_WHEN_LOCKED
or LayoutParams.FLAG_TURN_SCREEN_ON
or LayoutParams.FLAG_KEEP_SCREEN_ON
)

Note: Above code will show your activity in lock screen for API level <29 (Android 10).

For API level ≥29 you need to set fullscreen intent as below to show activity to your lock screen.

First of all, add permission in AnroidManifest.xml:

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

Now set full screen intent in your notification builder with your activity as below:

var answerCallIntent = Intent(this@FirebaseInstanceService, AnswerDeclineCallActivity::class.java)var answerCallPendingIntent = PendingIntent.getActivity(this@FirebaseInstanceService,
0, answerCallIntent, PendingIntent.FLAG_UPDATE_CURRENT)
setFullScreenIntent(answerCallPendingIntent, true)
Note : Some devices like Xiaomi, you have to redirect user to settings to enable "show on lock screen" permission to show activity in lockscreen.
show on lockscreen permission

Last but not least if you are using Firebase for push notification then set TTL (time-to-live) time from the backend so it will not trigger notification after the given time.

Let’s suppose if you are showing activity to the user for 15 seconds and if the device internet is off and then he/she turns on the internet after 15 seconds they will not get this push notification.

credit : https://giphy.com/gifs/reaction-winner-winning-KEVNWkmWm6dm8

I hope you are able to handle video call push notifications in every Android OS. Don’t forget to clap and share your feedback in the comment. Thanks.

--

--

--

Our Engineering blog gives an inside look at our technologies from the perspective of our engineers.

Recommended from Medium

SSL pinning on Ionic Android using ionic advanced http

All You Need To Know About Jetpack Compose

Jetpack Compose

Observe LiveData from ViewModel in Fragment

Dagger 2 Dependency Injection Part 1

Get 7865 Icons into Your Android App in 3 minutes

Fully customizable Jetpack Compose

A step-by-step guide to learn Kotlin for Android development

Static Analysis And Dynamic Analysis For Android Package Kit (APK)

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
Jaydeep Parmar

Jaydeep Parmar

More from Medium

Push notifications on android

Integrate Requestly SDK in your android application

Notes on Android Dev — Lecture 1 — Why/What/How Android

Basics for your first Android App: REST API, Database, and Fragment Navigation