How to manage incoming video call for every Android OS version with FCM notifications
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.
So, How to open activity when the app is killed or the device is locked?
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:
- When app is in the background
- When app is in Foreground
- When app is Killed
- 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:
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.
4. When device is locked
You can simply add below line in your activity tag in AndroidManifest.xml:
You also need to set Flags like below in the Java/Kotlin file of your activity:
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.
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.
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.