React Native and Firebase Dynamic Links
Tags: reactnative, firebase, mobile
Published: 2021-07-06 14:55:03

<!-- wp:heading -->
<h2>What are Dynamic Links?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Firebase Dynamic Links allow you to make a single link that can be handled on all devices, whether a user opens it on a desktop or a mobile phone. In the case of mobile phones, our app doesn't even have to be installed! </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>When a user opens a link without an app installed, he will be redirected to App Store/Google Play to install it. Our link can still be handled by the app when it is launched for the first time. This greatly increases the accessibility and the number of installs of our app because users can get the app "directly" from a link.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Why do I need it?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Let's say that you want to invite users to install your app with a promo code. You could send promo codes to users, and then they have to copy this code after signing up. It may seem like not much but this is an additional action the user has to take. And the more user has to do along the way, the higher the chance he won't do it at all. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Thanks to Dynamic Links, you can send a promo code in the form of a link. It will allow users to install the app if they don't have it installed and automatically apply the code, so the users only need to sign up. This way, we minimize the effort users have to take and maximize the chance they will install our app!</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There are many more use cases like allowing users to share content between them or even deeply integrating your app with your website – the same URL can either open your webpage or open your app. Possibilities are essentially limitless, and they make your app more appealing to the user.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>How to get started?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>First, we have to create a new project on Firebase or use existing if you already have one. I created a project called "dynamic-links-example". After you create your project and get into the Firebase console, you can find dynamic links under the "Engage" tab on left navigation menu. We can create a new URL prefix for our links by clicking the "Get started" button.<br></p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":5449,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://pagepro.co/blog/wp-content/uploads/2021/02/image-2.png" alt="Add URL prefix option in Firebase" class="wp-image-5449"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>After creating a domain, you should see a page like the one below. I created a domain called <em>exampledomain000.page.link</em> that Google provides. If you have your own domain, you can add it here too.<br></p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":5452,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://pagepro.co/blog/wp-content/uploads/2021/02/image-4.png" alt="Creating Firebase Dynamic Links menu" class="wp-image-5452"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>From this moment, we can create our dynamic links. There are two main ways of creating a dynamic link. You can either create them using the Firebase console by clicking "New Dynamic Link" button or generate them in your application.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Creating your links with the first method is mostly used when you want to share something with your users – a promo code, for example. The second option is used when you want to add custom parameters that can be different with each link. These links won't be visible in your Firebase console. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To create such links, we first need to add a pattern so we will be protected from phishing attacks. To add a pattern click on the dropdown next to the "New Dynamic Link" button and select "Allowlist URL pattern".</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":5455,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://pagepro.co/blog/wp-content/uploads/2021/02/image-5.png" alt="Creating a New Dynamic Links" class="wp-image-5455"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>I created a pattern that allows all links that look like "https://dynamic-links-example.com/&lt;any-params-here&gt;". Keep in mind that we added "<code>.*"</code> at the end. It allows us to add any params that we want to our links. For more details about how Regular Expressions work, check <a href="https://github.com/google/re2/wiki/Syntax" target="_blank" rel="noreferrer noopener nofollow">this link</a>.<br></p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":5456,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://pagepro.co/blog/wp-content/uploads/2021/02/image-6.png" alt="Allowlist URL pattern screenshot" class="wp-image-5456"/></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>Adding Firebase Dynamic Links to our app</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>After we are done with preparing firebase to handle our links, we can proceed to the next step. Which is adding <code>@react-native-firebase/dynamic-links</code> to our app. I will assume that you already have Firebase configured in your app. If not, please refer to this <a href="https://rnfirebase.io/" target="_blank" rel="noreferrer noopener nofollow">guide</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>First, we need to install a module mentioned above.</p>
<!-- /wp:paragraph -->

<!-- wp:code-mind/code {"code":"yarn add @react-native-firebase/dynamic-links\n\n# if you are using npm\nnpm install @react-native-firebase/dynamic-links\n\n# if you are developing app for ios\ncd ios/ \u0026\u0026 pod install","lang":"Bash"} -->
<pre class="wp-block-code-mind-code c-code"><code class="bash">yarn add @react-native-firebase/dynamic-links

# if you are using npm
npm install @react-native-firebase/dynamic-links

# if you are developing app for ios
cd ios/ &amp;&amp; pod install</code></pre>
<!-- /wp:code-mind/code -->

<!-- wp:heading {"level":3} -->
<h3>Configuring iOS</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Before we can start with configuring our iOS app to handle dynamic links, we need to have an Apple Developer account. Then we can create a provisioning profile for our app, and it has to have "Associated Domains" capability. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To check if your provisioning profile has this capability, click on the "i" icon next to your provisioning profile under the "Signing &amp; Capabilities" tab in Xcode and check if there is a capability called "Associated Domains".<br></p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":5462,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://pagepro.co/blog/wp-content/uploads/2021/02/image-7.png" alt="Signing and Capabilities tap in Xcode" class="wp-image-5462"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>When we have this capability in our provisioning profile, we can add it to our app by clicking the "+ Capability" button and selecting the "Associated Domains". Now we should see a new tab where we can add our domain. In our case, it should look like "applinks:exampledomain000.page.link".</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":5465,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://pagepro.co/blog/wp-content/uploads/2021/02/image-8.png" alt="Associated Domains tab" class="wp-image-5465"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>At this stage our iOS app is ready to receive dynamic links.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Configuring Android</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>To make our Android app handle Dynamic Links, we need to add an intent filter inside our MainActivity in the AndroidManifest.xml file.</p>
<!-- /wp:paragraph -->

<!-- wp:code-mind/code {"code":"        \u003cintent-filter android:autoVerify=\u0022true\u0022\u003e\n            \u003caction android:name=\u0022android.intent.action.VIEW\u0022/\u003e\n            \u003ccategory android:name=\u0022android.intent.category.DEFAULT\u0022/\u003e\n            \u003ccategory android:name=\u0022android.intent.category.BROWSABLE\u0022/\u003e\n            \u003cdata android:host=\u0022exampledomain000.page.link\u0022 android:scheme=\u0022http\u0022/\u003e\n            \u003cdata android:host=\u0022exampledomain000.page.link\u0022 android:scheme=\u0022https\u0022/\u003e\n        \u003c/intent-filter\u003e"} -->
<pre class="wp-block-code-mind-code c-code"><code class="javascript">        &lt;intent-filter android:autoVerify="true">
            &lt;action android:name="android.intent.action.VIEW"/>
            &lt;category android:name="android.intent.category.DEFAULT"/>
            &lt;category android:name="android.intent.category.BROWSABLE"/>
            &lt;data android:host="exampledomain000.page.link" android:scheme="http"/>
            &lt;data android:host="exampledomain000.page.link" android:scheme="https"/>
        &lt;/intent-filter></code></pre>
<!-- /wp:code-mind/code -->

<!-- wp:heading -->
<h2>Generating Dynamic Links</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>After all this configuration, we are finally ready to generate some links in our app. It is important that our <code>link</code> param is compatible with the URL pattern we made in the Firebase. Otherwise, it will be blocked.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can use two functions to generate links:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><strong>buildLink()</strong> – generates a link where all params are fully visible</li><li><strong>buildShortLink()</strong> – generates shortened link where parameters are hidden</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Both functions take the same params to build a link. Below there is an example of using <code>buildShortLink()</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:code-mind/code {"code":"const generateLink = async (param, value) =\u003e {\n  const link = await firebase.dynamicLinks().buildShortLink({\n    link: `\u003cyour_link\u003e/?${param}=${value}`,\n    ios: {\n      bundleId: \u003cbundle_id\u003e,\n      appStoreId: \u003cappstore_id\u003e,\n    },\n    android: {\n      packageName: \u003cpackage_name\u003e,\n    },\n    domainUriPrefix: 'https://exampledomain000.page.link',\n  });\n\n  return link;\n}"} -->
<pre class="wp-block-code-mind-code c-code"><code class="javascript">const generateLink = async (param, value) => {
  const link = await firebase.dynamicLinks().buildShortLink({
    link: `&lt;your_link>/?${param}=${value}`,
    ios: {
      bundleId: &lt;bundle_id>,
      appStoreId: &lt;appstore_id>,
    },
    android: {
      packageName: &lt;package_name>,
    },
    domainUriPrefix: 'https://exampledomain000.page.link',
  });

  return link;
}</code></pre>
<!-- /wp:code-mind/code -->

<!-- wp:heading -->
<h2>Handling Dynamic Links in app</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Finally, we are ready to handle dynamic links inside our <a href="https://pagepro.co/react-native-development.html" target="_blank" rel="noreferrer noopener">React Native</a> code. There are two cases when our app can get a dynamic link:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>The app was not running and has been launched by a dynamic link</li><li>The app has been launched in the background and is now moved to the foreground after receiving a dynamic link </li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>To handle the first scenario, we use the getInitialLink() method, which allows us to get a link that has launched our app. We can use this method anywhere in our app, and it will return the same link until the app is restarted or launched from a different link.</p>
<!-- /wp:paragraph -->

<!-- wp:code-mind/code {"code":"import {firebase} from '@react-native-firebase/dynamic-links';\n\nconst getAppLaunchLink = async () =\u003e {\n  try {\n    const {url} = await firebase.dynamicLinks().getInitialLink();\n    //handle your link here\n  } catch {\n    //handle errors\n  }\n};"} -->
<pre class="wp-block-code-mind-code c-code"><code class="javascript">import {firebase} from '@react-native-firebase/dynamic-links';

const getAppLaunchLink = async () => {
  try {
    const {url} = await firebase.dynamicLinks().getInitialLink();
    //handle your link here
  } catch {
    //handle errors
  }
};</code></pre>
<!-- /wp:code-mind/code -->

<!-- wp:paragraph -->
<p>Second scenario is handled by a listener like this:</p>
<!-- /wp:paragraph -->

<!-- wp:code-mind/code {"code":"import {firebase} from '@react-native-firebase/dynamic-links';\n\nconst unsubscribeDynamicLinks = firebase.dynamicLinks().onLink(({url}) =\u003e {\n  //handle your url here\n});\n\n//remember to unsubscribe when you dismiss your component to prevent memory leaks\nunsubscribeDynamicLinks();"} -->
<pre class="wp-block-code-mind-code c-code"><code class="javascript">import {firebase} from '@react-native-firebase/dynamic-links';

const unsubscribeDynamicLinks = firebase.dynamicLinks().onLink(({url}) => {
  //handle your url here
});

//remember to unsubscribe when you dismiss your component to prevent memory leaks
unsubscribeDynamicLinks();</code></pre>
<!-- /wp:code-mind/code -->

<!-- wp:paragraph -->
<p>And that's all! Now you can create, send and properly handle dynamic link with your users. And they can, too. :)</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Final notes</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Firebase Dynamic Links can add a lot to your app, which helps users interact and share data with others by sharing links more easily. These links preserve data even when they were launched before installing the app.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It also gives you the possibility to integrate with your website to add an option to open the same link on a web browser or app.  And all of this with not much work needed.</p>
<!-- /wp:paragraph -->