Create Dynamic Links on Android

You can create short or long Dynamic Links with the Firebase Dynamic Links Builder API. This API accepts either a long Dynamic Link or an object containing Dynamic Link parameters, and returns a URL like the following example:

https://abc123.app.goo.gl/WXYZ

Set up Firebase and the Dynamic Links SDK

Before you can create Dynamic Links in your Android app, you must include the Firebase SDK. If your app is set up to receive Dynamic Links, you have already completed these steps and you can skip this section.

  • 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:11.6.2'
  • 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.

Create a Dynamic Link from parameters

To create a Dynamic Link, create a new DynamicLink object with its Builder, specifying the Dynamic Link parameters with the Builder methods. Then, call buildDynamicLink or buildShortDynamicLink.

The following minimal example creates a long Dynamic Link to https://example.com/ that opens with your Android app on Android and the app com.example.ios on iOS:

DynamicLink dynamicLink = FirebaseDynamicLinks.getInstance().createDynamicLink()
        .setLink(Uri.parse("https://example.com/"))
        .setDynamicLinkDomain("abc123.app.goo.gl")
        // Open links with this app on Android
        .setAndroidParameters(new DynamicLink.AndroidParameters.Builder().build())
        // Open links with com.example.ios on iOS
        .setIosParameters(new DynamicLink.IosParameters.Builder("com.example.ios").build())
        .buildDynamicLink();

Uri dynamicLinkUri = dynamicLink.getUri();

To create a short Dynamic Link, build a DynamicLink the same way, but also specify your Dynamic Link domain (which you can find in the Firebase console) with the setDynamicLinkDomain method, and then call buildShortDynamicLink. Building a short link requires a network call, so instead of directly returning the link, buildShortDynamicLink returns a Task, which makes the short link available when the request completes. For example:

Task<ShortDynamicLink> shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
        .setLink(Uri.parse("https://example.com/"))
        .setDynamicLinkDomain("abc123.app.goo.gl")
        // Set parameters
        // ...
        .buildShortDynamicLink()
        .addOnCompleteListener(this, new OnCompleteListener<ShortDynamicLink>() {
            @Override
            public void onComplete(@NonNull Task<ShortDynamicLink> task) {
                if (task.isSuccessful()) {
                    // Short link created
                    Uri shortLink = task.getResult().getShortLink();
                    Uri flowchartLink = task.getResult().getPreviewLink();
                } else {
                    // Error
                    // ...
                }
            }
        });

You can use the Dynamic Link Builder API to create Dynamic Links with any of the supported parameters. See the API reference for details.

The following example creates a Dynamic Link with several common parameters set:

DynamicLink dynamicLink = FirebaseDynamicLinks.getInstance().createDynamicLink()
        .setLink(Uri.parse("https://example.com/"))
        .setDynamicLinkDomain("abc123.app.goo.gl")
        .setAndroidParameters(
                new DynamicLink.AndroidParameters.Builder("com.example.android")
                        .setMinimumVersion(125)
                        .build())
        .setIosParameters(
                new DynamicLink.IosParameters.Builder("com.example.ios")
                        .setAppStoreId("123456789")
                        .setMinimumVersion("1.0.1")
                        .build())
        .setGoogleAnalyticsParameters(
                new DynamicLink.GoogleAnalyticsParameters.Builder()
                        .setSource("orkut")
                        .setMedium("social")
                        .setCampaign("example-promo")
                        .build())
        .setItunesConnectAnalyticsParameters(
                new DynamicLink.ItunesConnectAnalyticsParameters.Builder()
                        .setProviderToken("123456")
                        .setCampaignToken("example-promo")
                        .build())
        .setSocialMetaTagParameters(
                new DynamicLink.SocialMetaTagParameters.Builder()
                        .setTitle("Example of a Dynamic Link")
                        .setDescription("This link works whether the app is installed or not!")
                        .build())
        .buildDynamicLink();  // Or buildShortDynamicLink()

Dynamic Link parameters

You can set Dynamic Link parameters with the following methods:

DynamicLink parameters
setLink

The link your app will open. You can specify a URL that your app can handle, typically an app's content/payload, which can initiate app-specific logic (such as crediting the user with a coupon or displaying a welcome screen). This link must be a well-formatted URL, be properly URL-encoded, use the HTTP or HTTPS, and not another Dynamic Link. scheme.

setDynamicLinkDomain Your Dynamic Link domain, which you can find in the Firebase console. A Dynamic Link domain looks like the following example: abc123.app.goo.gl
AndroidParameters
setFallbackUrl The link to open when the app isn't installed. Specify this to do something other than install your app from the Play Store when the app isn't installed, such as open the mobile web version of the content, or display a promotional page for your app.
setMinimumVersion The versionCode of the minimum version of your app that can open the link. If the installed app is an older version, the user is taken to the Play Store to upgrade the app.
IosParameters
setAppStoreId Your app's App Store ID, used to send users to the App Store when the app isn't installed
setFallbackUrl The link to open when the app isn't installed. Specify this to do something other than install your app from the App Store when the app isn't installed, such as open the mobile web version of the content, or display a promotional page for your app.
setCustomScheme Your app's custom URL scheme, if defined to be something other than your app's bundle ID
setIpadFallbackUrl The link to open on iPads when the app isn't installed. Specify this to do something other than install your app from the App Store when the app isn't installed, such as open the web version of the content, or display a promotional page for your app.
setIpadBundleId The bundle ID of the iOS app to use on iPads to open the link. The app must be connected to your project from the Overview page of the Firebase console.
setMinimumVersion The version number of the minimum version of your app that can open the link. This flag is passed to your app when it is opened, and your app must decide what to do with it.
NavigationInfoParameters
setForcedRedirectEnabled If set to '1', skip the app preview page when the Dynamic Link is opened, and instead redirect to the app or store. The app preview page (enabled by default) can more reliably send users to the most appropriate destination when they open Dynamic Links in apps; however, if you expect a Dynamic Link to be opened only in apps that can open Dynamic Links reliably without this page, you can disable it with this parameter. Note: the app preview page is only shown on iOS currently, but may eventually be shown on Android. This parameter will affect the behavior of the Dynamic Link on both platforms.
SocialMetaTagParameters
setTitle The title to use when the Dynamic Link is shared in a social post.
setDescription The description to use when the Dynamic Link is shared in a social post.
setImageUrl The URL to an image related to this link. The image should be at least 300x200 px, and less than 300 KB.
GoogleAnalyticsParameters
setSource
setMedium
setCampaign
setTerm
setContent
Google Play analytics parameters. These parameters (utm_source, utm_medium, utm_campaign, utm_term, utm_content) are passed on to the Play Store as well as appended to the link payload.
ItunesConnectAnalyticsParameters
setProviderToken
setAffiliateToken
setCampaignToken
iTunes Connect analytics parameters. These parameters (pt, at, ct) are passed to the App Store.

Shorten a long Dynamic Link

To shorten a long Dynamic Link, specify the URL of the Dynamic Link using setLongLink instead of setting parameters with the other builder methods:

Task<ShortDynamicLink> shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
        .setLongLink(Uri.parse("https://abc123.app.goo.gl/?link=https://example.com/&apn=com.example.android&ibn=com.example.ios"))
        .buildShortDynamicLink()
        .addOnCompleteListener(this, new OnCompleteListener<ShortDynamicLink>() {
            @Override
            public void onComplete(@NonNull Task<ShortDynamicLink> task) {
                if (task.isSuccessful()) {
                    // Short link created
                    Uri shortLink = task.getResult().getShortLink();
                    Uri flowchartLink = task.getResult().getPreviewLink();
                } else {
                    // Error
                    // ...
                }
            }
        });

Send feedback about...

Need help? Visit our support page.