Receive Firebase Dynamic Links on Android

To receive the Firebase Dynamic Links that you created, you must include the Dynamic Links SDK in your app and call the getInvitation method when your app loads to get the data passed in the Dynamic Link.

Set up Firebase and the Dynamic Links SDK

  • Add Firebase to your Android project. Add the dependency for Firebase Dynamic Links to your app-level build.gradle file:
    compile 'com.google.firebase:firebase-invites:10.2.1'
  • If you haven't yet connected your app to your Firebase project, do so from the Firebase console.
  • In the Firebase console, open the Dynamic Links section.
    1. Accept the terms of service if you are prompted to do so.
    2. Take note of your project's Dynamic Links domain, which is displayed at the top of the Dynamic Links page. You need your project's Dynamic Links domain to programmatically create Dynamic Links. A Dynamic Links domain looks like app_code.app.goo.gl.

Add an intent filter for deep links

As with plain deep links, you must add an intent filter to the activity that handles deep links for your app. This is required for your app to receive the Dynamic Link data after it is installed from the Play Store. In AndroidManifest.xml:

<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="example.com" android:scheme="http"/>
    <data android:host="example.com" android:scheme="https"/>
</intent-filter>

When users open a link to the scheme and host you specify, your app will start an activity to handle the link.

Handle deep links

To receive the deep link, call the getInvitation method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // ...

    // Build GoogleApiClient with AppInvite API for receiving deep links
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, this)
            .addApi(AppInvite.API)
            .build();

    // Check if this app was launched from a deep link. Setting autoLaunchDeepLink to true
    // would automatically launch the deep link if one is found.
    boolean autoLaunchDeepLink = false;
    AppInvite.AppInviteApi.getInvitation(mGoogleApiClient, this, autoLaunchDeepLink)
            .setResultCallback(
                    new ResultCallback<AppInviteInvitationResult>() {
                        @Override
                        public void onResult(@NonNull AppInviteInvitationResult result) {
                            if (result.getStatus().isSuccess()) {
                                // Extract deep link from Intent
                                Intent intent = result.getInvitationIntent();
                                String deepLink = AppInviteReferral.getDeepLink(intent);

                                // Handle the deep link. For example, open the linked
                                // content, or apply promotional credit to the user's
                                // account.

                                // ...
                            } else {
                                Log.d(TAG, "getInvitation: no deep link found.");
                            }
                        }
                    });
}

If the launchDeepLink parameter is true, the app broadcasts the deep link, which can be handled by the activity to which you added the intent filter. If the launchDeepLink parameter is false, you can manually start the intent returned by getInvitationIntent to handle the deep link when appropriate.

You must call getInvitation() in every activity that might be launched by the link, even though the link might be available from the intent using getIntent().getData(). Calling getInvitation() retrieves the link and clears that data so it is only processed once by your app.

You normally call getInvitation() in the main activity as well as any activities launched by intent filters that match the link.

Record analytics

The following events can be automatically tracked in Firebase Analytics and shown in the Firebase console.

  • dynamic_link_app_open
  • dynamic_link_first_open
  • dynamic_link_app_update

In order to register these events, you need to configure Firebase Analytics before you retrieve the deep link. Check the following conditions are met:

  • Call AppInviteApi.getInvitation() in your app entry points:
    • Launcher activities. e.g.: action="android.intent.action.MAIN", category="android.intent.category.LAUNCHER".
    • Activity entry points. e.g.: onStart(), onCreate().
    • Deep link activities.
  • Set up and use Firebase Analytics:
    • Include the Firebase Analytics dependency. This is usually automatically added by the google-services Gradle plugin.
    • Include the google-services.json config file in your app.
    • Call FirebaseAnalytics.getInstance() before calling AppInviteApi.getInvitation().

Send feedback about...

Need help? Visit our support page.