Exploring Firebase on Android: Dynamic Links

Firebase is such an exciting new collection of services that I’ve been reading up on and experimenting with. In this new series of articles, we’ll be covering the features of firebase to learn exactly what we can do with each integration. In this chapter, we’re going to be taking a look at Firebase Dynamic Links — the integration that allows us to provide deep links that have the ability to survive the installation process and more!


Don’t forget to check out the previous article in this series:


Deep links aren’t anything new. But incase you’re not aware, they provide us with the ability to link our users directly to content within our app. For example, say we wish to link the user to a specific screen in our app (say you have a product page and you wish to link a user to a specific product within your app from an external source) then you can create a deep link to achieve this functionality. But what happens if the user doesn’t have our app installed? Well in this case, the deep links won’t quite work (that’s a bit of a problem, right?).

Luckily for us, Firebase features what are called Dynamic Links — these links are pretty much the same as deep links, except they’re able to survive the install process. So if the user clicks one of these links without having our app installed, they will be taken to install the app and upon completion will be navigated to the deep link that has been assigned to the dynamic link.

This is a huge improvement on deep linking, so let’s dive a little deeper into dynamic links so we can learn what we can do with them and how!


I’m also releasing a full eBook that will act as a practical guide to integrating firebase features, which will feature more detailed guides on each section of the Firebase suite. Click the image below to be alerted when it’s out!


What can we do with Firebase Dynamic Links?

As previously mentioned Dynamic Links offer our applications a much more powerful way of linking to content within our apps. They’re basically deep links on steroids with the ability to:

Persist context across application install

If the user clicks a dynamic link and the the link requires your application to be installed, then the linked will automatically be opened once the application has been installed.

Persist context across application upgrades

On Android, if the clicked dynamic link requires an upgraded version of our application, then just like the last point our application will automatically handle the dynamic link once the application has finished installing.

Behave just like normal Links

In cases where the application doesn’t require installation (say, if it’s already installed) then clicking the Dynamic Link will automatically open the link to the desired screen.


From the above points you can see that Dynamic Links have a very simple process flow:

  • The user begins by clicking the Dynamic Link
  • If the the needs of the Dynamic Link target are satisfied (this is, the application being installed) then the user is navigated to the target location
  • Otherwise, if the application requires install in order to navigate to the Dynamic Link target, the the user is taken to the point of install for the application. Once the application has been installed, the user is navigated to the target location of the Dynamic Link

And if that wasn’t all, we can integrate Dynamic Links with Firebase Analytics to track the interaction with any links that we generate for our applications. But if we only require simple tracking, then we can use the automatic built-in analytics from the Dynamic Links panel within the Firebase Console where we can also obtain attribution and referrer information for interacted links with no extra effort required from our side.


Creating Dynamic Links

Before we get started, we’re going to begin by creating a Dynamic Link from within the Firebase Console. Let’s begin by navigating to the Dynamic Links section here:

https://console.firebase.google.com/project/YOUR_PROJECT_ID/durablelinks/links/

Once you’ve navigated this page you’ll need to begin by taking note of your Dynamic Link domain — you’ll need this in order to programatically create dynamic links (if you wish to do so!). You’ll find it here:

Now you’ve done that, let’s go ahead and click that GET STARTED button on the Dynamic Links card.

Generate Dynamic Link

We’re now presented with two options, at this point we want to Generate a Dynamic Link — if this option isn’t already selected then click the radio button to be shown the form for creating a dynamic link.

We begin by filling out the first two properties for the form:

  • Link name — This is the name which you’ll be using to define the link, be sure to make it short and descriptive. This name will be used when you’re tracking data related to the dynamic link
  • Link URL — The deep link in which the application will open for this link

Note: Both of these are required before we can continue.

Secondly we need to define the behaviour for iOS apps. For now we’re going to just open the browser as per normal, we’ll look at the second option in the next step when dealing with the Android implementation.


We’re then asked to fill out the same details for the behaviour on Android. Here, we’re going to open a deep-link for our link behaviour.

Once selecting the app that you want to apply the behaviour to, we need to select where to send the user to if our app is not installed. We can either:

  • Send the user to our Google Play page to install our application
  • Send them to the deep link URL
  • Send them to a custom URL of our choosing

In this case where the application isn’t installed, it makes sense to forward the user to Google Play so that they can installed our app and continue to the deep link once the installation process has completed.

However, selecting the Deep Link URL option will open your defined deep link in the users mobile browser. Finally, selecting Custom URL allows you to enter a URL to direct the user to when your application isn’t installed.

For each of these options there’s also an optional setting to state the maximum version of your application which the option should be applied to. If you don’t set this then the option which you select will be applied to all application versions.


Next we can set some properties that allow us to track analytics for the engagement of our links:

This is completely optional, but if we wish then we have the ability to set:

  • The campaign source — this is the name of the site, publication etc which the traffic for the link is coming from
  • The campaign medium — this is the marketing / advertising medium for the campaign, this could be something such as a banner ad or email newsletter
  • The campaign name — this is the name of the campaign which you are tracking, this could something such as a promo code or some other specific identifier

All of these are completely optional and are not required to be set.


Finally, we can now add some social meta tags to our dynamic link if we wish. Setting these will allow us to generate a preview to be shown on social networks when our link is being shared. Not setting these will mean that the corresponding social network will generate and use their own meta tags for your dynamic link.

Here we can set the:

  • Preview title — this is the title of the link which is being shared
  • Preview image URL — this is a URL for an image which identifies the link which is being shared
  • Preview description — this is a description used for provided information about the link being shared

Again, all of these are completely optional and are not required to be set.


Handling Dynamic Links in Android

Now we know a little about how firebase dynamic links work, lets take a look at how we can handle them within our app! This process is pretty simple and only requires a few steps 🙂

Add the dependancy

We need to being by adding the dependancy for firebase invites to our app build.gradle file:

compile 'com.google.firebase:firebase-invites:9.8.0'

Once this has been added, we now have access to the parts of the firebase API that allow us to handle dynamic links within our application.

Add intent filter for deep links

Next, we need to add an intent filter to our manifest file so that we can handle deep links — this allows our application to receive the data for the dynamic link after the application has been installed from the playstore.

https://gist.github.com/hitherejoe/89503ba54fa1923c881b358e8aaba5fc

The host and scheme attributes above are used to state which values will cause our app to be launched. Here, the activity that has registered this intent filter is registered to be notified when the app receives a deep link for joebirch.co .

Handling deep links

So that we can handle deep links, we need to begin by instantiating an instance of the GoogleApiClient class. When doing this, we need to enable the App Invite API, you can see below that we pass in a reference to this api when building the Google Api Client instance.

https://gist.github.com/hitherejoe/8a1e077fc43f0a9225ab2aa03d4d0f25

Once we have our instance, we can then use the getInvitation() method to catch any dynamic links when the user opens the app after it’s been installed from a clicking a dynamic link. You should place this method call within an activity that would be launched by a link, this could be either:

  • The main activity for when the user opens the application after installation.
  • Any activities that have an intent filter matching that of the deep link.

https://gist.github.com/hitherejoe/b1dc9afba2ded8088012f726eec5d734

You’ll notice that there is a third parameter to the getInvitation() method — this a boolean value which allows us to state whether the link should be automatically broadcasted once it has been retrieved. If this is set to false however, then calling getInvitationIntent() from the AppInviteInvitationResult instance will allow us to retrieve the intent and start it when we wish.

The flow here only consists of a few parts:

  • If the user installs our app from a dynamic link then the main activity of our app opens from the invitation.
  • This activity calls the getInvitation() method and fetches the invitation. Once the invitation has been received, the method will update the invitation state to installed and pass us a reference to the AppInviteInvitationResult instance.
  • We can use the getInvitationIntent() from this instance to fetch both the invitation ID and the deep link which is to be launched.
  • If we’ve set auto-launch boolean value in our getInvitation() method call to true then the app is re-launched using the deep link. If this deep link points to another activity, then that activity is launched.
  • Otherwise, if we have this auto-launch value set to false then our app is responsible for launching the intent when we see fit.
  • However, if the app was already installed when the user clicked the dynamic link then the re-launching of the application will not take place. This is because the invitation data with the AppInviteInvitationResult will be used to launch the corresponding activity.

And that’s it!

So we’ve seen what we can do with Firebase Dynamic Links and how to implement them to improve the deep linking experience. I hope from this you’ve been able to see the benefits of Firebase and how super easy it is to get setup!

And if you wish to learn more about Firebase Dynamic Links and other integrations, please do remember to sign-up to be alerted when my Firebase eBook is out! 🚀

Using Firebase or have questions about it? Leave a reply to this post or send me a tweet!

Check out some of my other projects at joebirch.co

Leave a Reply

Your email address will not be published. Required fields are marked *