How to Implement Foreground Service and Deep Links for Android apps with calls? With Code Examples

How to create a Foreground Service on Android

Today’s smartphones and their operating systems have many built-in optimizations aimed at extending battery life. And mobile app developers need to keep in mind the potential actions the system can take on the app.

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
class OngoingCallService : Service() {@Injectlateinit var abstractCallManager: AbstractCallManager// Implementation of an abstract method; we won’t use Bind so just return nulloverride fun onBind(intent: Intent): IBinder? = null}
<service// Class name of our serviceandroid:name=".OngoingCallService"android:enabled="true"// This flag meant that other applications can’t run this serviceandroid:exported="false"// Declare a type of our serviceandroid:foregroundServiceType="microphone|camera|phoneCall" />
private fun startForegroundService() {val intent = Intent(this, OngoingCallService::class.java)ContextCompat.startForegroundService(this, intent)}
val notification = getNotification()startForeground(ONGOING_NOTIFICATION_ID, notification)
private fun stopForegroundService() {val intent = Intent(this, OngoingCallService::class.java)stopService(intent)}
abstractCallManager.isInCall.collect { if (it) startForegroundService() else stopForegroundService() }

Android Deep Links Tutorial

An extremely user-friendly feature that simplifies the growth of the user base of the app is the links to a certain place in the app. If the user doesn’t have the app, the link opens a page on Google Play. In the context of call apps, the most successful use case is the ability to share a link to a call / meeting / room. The user wants to talk to someone, throws the link to the person he’s talking to, that person downloads the app, and then gets right into the call — what could be more convenient?

dependencies {implementation 'com.google.firebase:firebase-dynamic-links:20.1.1'}
<activity ...><intent-filter>// These action and category notify the system that we can “display” the links<action android:name="android.intent.action.VIEW"/><category android:name="android.intent.category.DEFAULT"/><category android:name="android.intent.category.BROWSABLE"/>// Description of the link which we can handle. In this case these are the links starting from calls://forasoft.com<dataandroid:host="forasoft.com"android:scheme="calls"/></intent-filter></activity>
Firebase.dynamicLinks.getDynamicLink(intent).addOnSuccessListener(this) { data ->val deepLink: Uri? = data?.link}
val deepLink = intent?.data
val meetindId = deepLink?.getQueryParameter("meetingid")if (meetingId != null) abstractCallManager.joinMeeting(meetingId)

Conclusion

In the final article of our cycle “what each application with calls should have” we’ve gone through keeping our application alive after minimizing it and using the deep links as a convenient option for invitations to the call. Now you know all the mechanisms that make the user experience better not only inside the application but also at the system level.

--

--

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
Fora Soft

Fora Soft

Video and Multimedia Software Developmet company