Software Development

How to handle deeplink in app?

admin
3 October 2023
How to handle deeplink in app?

Add Intent Filter in Splash Activity



<intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:host="thestarrchefdev.page.link" android:scheme="https" /> <data android:host="thestarrchef.page.link" android:scheme="https" /> <data android:host="www.thestarrchef.com" android:scheme="http" /> </intent-filter>

In Splash Activity:

try {
    FirebaseDynamicLinks.getInstance()
        .getDynamicLink(intent)
        .addOnCompleteListener { task ->
            if (task.isSuccessful) {
                val pendingDynamicLinkData = task.result
                when {
                    pendingDynamicLinkData != null -> {
                        checkAppData(pendingDynamicLinkData.link?.query)
                    }
                    intent.data != null -> {
                        val data = intent.dataString?.split("?")
                        if (data?.size ?: 0 >= 2) {
                            checkAppData(data = data?.get(1) ?: "")
                        } else {
                            checkAppData("")
                        }
                    }
                    else -> {
                        saveReferral(AppConfig.ORGANIC_INSTALL_QUERY)
                        checkAppData(null)
                    }
                }
            } else {
                checkAppData(null)
            }
        }
} catch (ex: Exception) {
    ex.printStackTrace()
}

How to generate & send Deep Link?

fun sendContent(
    context: Context, postId: String?, title: String?,
    packageName: String = "",
    businessPost: Boolean = false
) {
    val link = "${AppConfig.BASE_URL}$postId"
    FirebaseDynamicLinks.getInstance().createDynamicLink()
        .setLink(Uri.parse(link))
        .setDomainUriPrefix(BuildConfig.DynamicLink)
        .setAndroidParameters(
            DynamicLink.AndroidParameters.Builder(BuildConfig.APPLICATION_ID)
                .setMinimumVersion(AppConfig.MIN_VERSION_FOR_INVITE)
                .build()
        )
        .setIosParameters(
            DynamicLink.IosParameters.Builder(BuildConfig.iOS_app_id)
                .setAppStoreId(BuildConfig.iOS_store_id)
                .setMinimumVersion(BuildConfig.iOS_min_version)
                .build()
        )
        .buildShortDynamicLink()
        .addOnSuccessListener { shortDynamicLink ->
            val shareLink = shortDynamicLink.shortLink
            val msg =
                "Hey, Check out this amazing ${if (businessPost) "content" else "recipe"} ($title) on The Starrchef App. Download app now!!\n\n$shareLink"
            shareText(context, String.format(msg, shareLink.toString()), packageName)
        }.addOnFailureListener { e ->
            shareText(
                context,
                "Hey, Check out this amazing ${if (businessPost) "content" else "recipe"} ($title) on The Starrchef App. Download app now!!\n\n${AppConfig.APP_URL}"
            )
        }
}

Table of Contents

Recent Comments
    December 2024
    M T W T F S S
     1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031  
    Tags:
    androidcoroutineskotlinmobile app development
    1 like
    Leave a Comment
    Share:
    Social