Deep Link Support

Prev Next

Overview

This guide explains how to enable deep link handling in Android applications using the Mapp Engage Android SDK v7.

Deep links allow your app to respond to specific URIs triggered by push notifications or other external sources. In the Mapp Engage context, deep links are typically used to:

  • Open specific screens within the app

  • Route users based on push notification content

  • Handle custom navigation logic tied to a message

This guide covers:

  • Creating a dedicated DeepLinkActivity

  • Handling and validating incoming intents

  • Declaring the activity in the AndroidManifest.xml


Step 1: Create and Configure the DeepLinkActivity

The DeepLinkActivity is responsible for:

  • Receiving incoming deep link intents

  • Validating the intent action and package

  • Extracting relevant query parameters (link, messageId, etc.)

  • Displaying the extracted data

  • Attempting to open the link in an appropriate activity

  • Handling unsupported intents gracefully

1.1 Implement the DeepLinkActivity Class

Create a new activity in your library and add the following code:

class DeepLinkActivity : AppCompatActivity() {

    private lateinit var binding: ActivityDeepLinkBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityDeepLinkBinding.inflate(layoutInflater)
        setContentView(binding.root)
        handleIntent(intent)
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        handleIntent(intent)
    }

    private fun handleIntent(intent: Intent) {
        val action = intent.action ?: return
        val packageName = intent.`package` ?: return
        val data = intent.data ?: return

        if (action == Actions.MAPP_DEEP_LINK_ACTION && packageName == this.packageName) {
            val scheme = data.scheme
            val authority = data.authority
            val link = data.getQueryParameter("link")
            val messageId = data.getQueryParameter("messageId")?.toLongOrNull()

            val sb = StringBuilder().apply {
                append("scheme: ").append(scheme).append("\n")
                append("authority: ").append(authority).append("\n")
                append("path: ").append(link).append("\n")
                append("messageId: ").append(messageId)
            }

            binding.tvDeepLinkUri.text = sb.toString()
            binding.btnOpenDeepLink.setOnClickListener {
                val openingIntent = Intent(Intent.ACTION_VIEW, Uri.parse(link))
                if (openingIntent.resolveActivity(packageManager) != null) {
                    startActivity(openingIntent)
                    finishAfterTransition()
                } else {
                    Util.showDialog(this, "Unsupported intent", "No activity can handle provided deep link!")
                }
            }
        }
    }
}

Key Features of the Implementation:

  • Intent Handling: Processes both onCreate() and onNewIntent() to support different launch modes.

  • Validation: Ensures the intent action and package match the expected values.

  • Parameter Extraction: Retrieves scheme, authority, link, and messageId from the URI.

  • UI Feedback: Displays deep link data for debugging or demonstration purposes.

  • Navigation Logic: Attempts to open the extracted link using an ACTION_VIEW intent.

  • Fallback Handling: Shows a dialog if no activity can handle the provided deep link.


Step 2: Declare the Activity in AndroidManifest.xml

To allow your deep link activity to be triggered via external intents, declare it in your application’s AndroidManifest.xml:

<activity
    android:name=".DeepLinkActivity"
    android:exported="true"
    android:taskAffinity="">
    
    <intent-filter>
        <!-- Deep link scheme -->
        <data android:scheme="apx" />
        <data android:host="deeplink" />

        <!-- Custom action to trigger this deep link -->
        <action android:name="com.appoxee.VIEW_DEEPLINK" />

        <!-- Categories for deep linking -->
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <category android:name="${applicationId}" />
    </intent-filter>
</activity>

Explanation of the Intent Filter:

  • android:exported="true" allows the activity to be launched by external components.

  • The <data> elements define the expected deep link structure (for example: apx://deeplink).

  • The <action> ensures that only intents with the matching custom action are handled.

  • The DEFAULT and BROWSABLE categories allow the activity to be invoked from both internal and browser-based sources.

  • The ${applicationId} category restricts resolution to the current application.