Getting Started with Firebase Dynamic Links for Android

Kasem SM
Firebase Developers
8 min readNov 13, 2021
Photo by Kelly Sikkema on Unsplash

🤔Why does your app need a Share Button?

Well, there is no need to say how much your app needs a Share this Post/Product/Item button because you know what, by the time you read till here, thousands of people may already have used the Share Link option on your favourite social media platform.

For instance, let us assume that I am the owner/developer of a social media platform and one of my users wants to share a post that contains an image with their friend who is not using my app; they can use the share button. It has many benefits, such as there is a higher chance my user’s friend will also install the app considering that the link will redirect them to install my app or my website. If a proper preview of the content (for example, the image) shared is visible, it will be a good experience for my future users.

(PS: many social media apps such as WhatsApp, Instagram, Telegram, and even Twitter displays a preview of the link you share them)

⚒️Complexity level of Implementing this feature?

✨Easy

🔗Github Project Link

🙋‍♂️Common FAQs

Should I use Firebase for this feature? If yes, why?

I recommend using Firebase for this feature as this service of Firebase (Dynamic Links) is entirely free. If you wish not to use Firebase, you will miss the following features:

  1. If a user does not have your app installed and they click the shareable link (which they may have received from their friends), simply nothing will happen, and it is a loss for you and a bad experience for your future users. (TBH, I would never install the app manually & click the link again when it gets installed 😴) Therefore, this is one of the reasons why you should use Firebase instead, as it will automatically redirect either to install your app on the Google Play Store or to your website.
  2. Secondly, there would be no ability to show a preview image of the meme shared by my user with their friend, and again it is a bad experience for my user and their friend. (At this point, if you think why should you care about your user’s friend who has not even installed your app yet, you should think again)
  3. Thirdly, if you do not use Firebase, your link will contain some (maybe sensitive) actual data, like the id of your post/product/item, and your user can try to manipulate the link (by changing the id or similar kinds of stuff) and play with it.
  4. Last but not least, if you do not use Firebase for this feature, you may miss important analytics of your links such as total clicks.

👩‍💻Let’s start the implementation

Firstly I assume that you have already connected your Android app with Firebase. If you do not know how to do so, please refer to the official documentation.

So till now, you have already connected your app with Firebase, and you probably have the Firebase Console opened in your browser, so now have a look at your project’s dashboard. [The link may look like https://console.firebase.google.com/project/$yourProjectID/overview] and when you glance to the left of the dashboard, you will see a navigation menu.

At the top, you’ll see a settings icon. Expand it and click on Project Settings. Now scroll towards the bottom of your page and click Add Fingerprint.

You’ll have to add your debug as well as a release SHA-256 key if you generate a release app bundle. To generate a debug key, open your Android Studio Project, tap the Control/Command key twice and rungradle signingReport.

You’ll see something like this:

Copy theSha-256 key and enter it into the Firebase console.

(Please note, this will not work in the release version of your app. You’ll need to generate the SHA-256 from the keystore file you used to sign your app bundle/APK). Refer to this Stack Overflow question here.

Try expanding the Engage category in which there is an option called Dynamic Links. (If you are lazy enough, use this link and replace your project ID, and you are good to go https://console.firebase.google.com/project/$yourProjectID/durablelinks)

You will see this screen:

Once you click the Get Started button, a 4-step dialog will popup, and it is time to add your domain. (You do not own a custom domain name? Do not worry! Just type your desired domain and use the Google Provided Domains (probably .page.link) — it is entirely free ✨ If you have used Google provided domains, you will not need to follow other remaining procedures, and if you have used a custom domain, you may need to follow the on-screen instructions.

At this stage, you should see this screen:

Click the New Dynamic Link button. You will be redirected to a new page, titled Create. Fill in the first two options. When you are at step three, you can either choose to redirect to the website you added in step two for any person who clicks your dynamic link from an iOS device, or you can also open your iOS app on the App Store if you have one.

The fourth option is the Deep Link behavior for Android. Again, you have two options, which are all self-explanatory. Choose your app’s package name at Open the deep link in your Android app and define where the link should redirect if the user does not have your app installed. In my case, I will redirect them to Google Play Store.

Click Next, and finally, click the Save button.

Congratulations for completing the first part!

👨‍💻Time to write code

Open your Android Project; in my case, I have already created a dummy app that contains two screens, a screen where the user’s post will get displayed with a share button, and another screen to view the post in detail. You can find out the project here on Github. If you are cloning the repository, please make sure to change the package name of the cloned repo with the package name you have created your Firebase Project. Also, don’t forget to update the google-services.json file.

Let’s first add the required dependency. You can get the latest version of the dependencies here. As of this time, the latest version of dynamic links dependency is 21.0.0.

Now, let us create a constants file. Firstly you will need your URL (the domain name you added when setting up Dynamic Links at Firebase Console). You can find it in the Dynamic Links dashboard.

Firebase Dynamic Links Dashboard

Now let us use the Firebase Dynamic Links library to create a shareable link.

The first parameter of this function, generateSharingLinkis a deeplinkof type Uri. You will come to know what this is when we will use this function in practice. The second parameter is a function that takes in a String, so after successful generation of the sharing link, we will pass the link as String in here.

Well, this was easy. Do you also wish to add an image preview in your link?

Let us do that too:

This was easy; we only added a new parameter called setSocialMetaTagParameters that takes in your preview image link.

Generally, the generated dynamic links are too long, and if you wish to generate a shorter link, you can use the following code:

Instead of adding buildDynamicLink() at the end to construct, we use buildShortDynamicLink(). Now, as the type would be of Task<ShortDynamicLink>, we can opt in to use the Success and Failure listeners.

We will call this function whenever a user clicks the share button.

So at the start, when we were building the generateSharingLink function, I mentioned that you would come to know what is this deeplink (first parameter) when we use this function in practice 🙋‍. It is the place where we have to construct our sharing link manually; provided that it should contain the prefix link at start (which we added in our Constants.kt) appended by a (‘/’) slash, and further you can provide the ID or any other unique identifiers of your post/product/item.

In our case, an example of the link should look like this:

https://myarticle.page.link/post/210, where https://myarticle.page.link is our prefix, 210 is the post ID. the/post/ is only necessary if you have multiple shareable items such as user post, user profile, etc.

Do not worry, we will not expose this ID as Firebase will replace everything after page.link/ with some random strings or numbers. ✨

We now have three more pieces of code to write:

  1. Open the sharing dialog after the link is successfully generated.
  2. Declare an intent filter in our AndroidManifest.xml file.
  3. Handle the part when a user clicks on the link where they expect to see the post their friend shared.

🔗Open Sharing Dialog

(after successful generation of the link by Firebase)

✏️Declaring an Intent Filter in our Android Manifest:

Why was this added? In simple words, we are letting our app know that whenever a user clicks on the link (in my case,https://myarticle.page.link), this app should open, provided that this app is installed. If not, Firebase will handle it by redirecting your user to install it from Google Play Store, or it will open the website you mentioned when setting up the Dynamic Link initially. (That is exactly the behaviour we want! 🤗)

👆Handling the last and the most important part

(When a user clicks on the link where they expects to see the post their friend shared.)

Again, this will be pretty simple. FirebaseDynamicLinksmakes it very easy to know if the app is opened after clicking the shareable link.

So basically, the process would be: We will have an onSuccess listener (after initializing Firebase Dynamic Links) that returns a variable of type PendingDynamicLinkData which contains the actual link that was manually created by us when generating a sharing link (though our users will only see the generated link by Firebase that contains random strings and numbers)

Let’s see this in practice!

You should always do this in the Activity where you have added the intent filters. In my case, I have added the filters into my MainActivity.kt.

✨ Congratulations, you have successfully implemented this feature in your app.

That’s it. if you have any questions related to this article, you can connect with me on Twitter anytime. Checkout this project on Github.

Resources

--

--