SUMIT CHANDEL: Hello again.
Last video in this series, we covered
how to configure your Firebase project to enable Firebase
Dynamic Links and then we created our very first Firebase
Dynamic Link in the console.
Now, let's do something more interesting
and actually receive and interpret a Dynamic Link
in our Android app.
Oh, and as a bonus, let's also cover
how to use Firebase Dynamic Links alongside your already
configured App Links in your Android app in this video
as well.
It's going to be a fun-filled Firebase ride.
So let's get started on this episode of Firecasts.
[MUSIC PLAYING]
Now, receiving a Dynamic Link in your app
is actually quite easy.
The way it works is once a Dynamic Link is clicked,
that link gets passed into your app,
along with the Firebase Dynamic Link data, including
the deep link parameter.
Based on that deep link parameter,
and which activity is configured to handle
the URL in that deep link parameter,
your Android app will open to the correct activity
when a user clicks on a Dynamic Link.
So let's see how we make those configurations
to receive the Dynamic Link and open the app
to the correct activity.
First, let's make sure we've already
added the Firebase Dynamic Links SDK to your Android app.
This requires adding both the Firebase Core libraries
and the Firebase Dynamic Links library
to our app level build.gradle file.
At the time of this recording, this
would be Firebase Core 16.0.5 and Firebase Dynamic Links
16.1.5.
So I'll add those in here.
But you'll want to be sure to check the documentation
for the latest version at the time
that you're watching this video.
Next, remember the Dynamic Link we've
created in the console and the deep link parameter
that we set up for it?
Well, we'll need to configure which activity we would like
to handle that deep link URL contained within that Dynamic
Link.
And we can do that by configuring that information
in our Android manifest file via intent
filters on our activities.
So let me start with my main activity, which for my purposes
will handle all Dynamic Links that lead to my app.
So, I'm going to add this new intent
filter with a view action and a default and browsable category.
And then I'm going to add these data
tags that will tell it which deep link URLs
it should handle.
In this case, I want this to handle any URLs that
have a host of example.com.
And just to be safe, I'll make sure it can handle both
the HTTP and HTTPS schemes.
OK, so with this set, my app is now
configured to handle all Dynamic Links with a deep link
parameter set with the example.com domain, which
is perfect for my example.
In your own app though, you'll want
to set up intent filters with different host URL
patterns for each activity where it makes sense
to receive a Dynamic Link.
You'll also need to set a meaningful deep link
URL for each Dynamic Link that corresponds
to the matching activity that you want to open
when that link is clicked.
So for example, a Dynamic Link that ends with slash promo
will probably have a different deep link
parameter than a Dynamic Link ending with slash welcome.
And you'll want to send users to different activities,
depending on which link they came from.
OK, so at this point, we have the mapping
for which Dynamic Links will launch which activities based
on their deep link URL.
Now, let's take a look at actually extracting that link
within an activity.
For my example, I only have one activity
receiving Firebase Dynamic Links, my main activity.
So I'll look at adding code to receive and handle
the dynamic link here.
The way I can get the Firebase Dynamic Link
is by making a call to the Firebase Dynamic Link
libraries, like so, within my onCreate method.
So let me type out that method call.
It would be FirebaseDynamicLinks.getInstance
.getDynamicLink and to that I'll call get intent and then add
an onSuccess listener.
Now there are a few important things
to explain in these calls to understand
what's happening before moving on to my link handling code.
So let's step through them.
The first is the FirebaseDynamicLinks.getInstance
method call.
So in addition to providing us with a singleton instance
we can use to call into our Firebase Dynamic Links SDK
methods, this call is also what enables Firebase Dynamic Links
analytics tracking in Google Analytics for Firebase.
So if you have analytics configured for your Firebase
project, this getInstance call ensures
that the events associated with interacting with the Firebase
Dynamic Link are reported.
Next, is the getDynamicLink get Intent call.
Now, this is an asynchronous call
that returns a task object sometime later after fetching
the Dynamic Link data from the Firebase Dynamic Link servers.
This call is important because it's actually
what checks for any pending Dynamic Link data that
was captured if we're coming to this activity
from a user clicking on a Dynamic Link.
And it might also involve doing some extra steps,
like resolving the short link URL to the expanded URL
that you'll need to extract your deep link data.
Since this call is asynchronous, a returns a task
object on which we need to add success and failure listeners.
These will be called once it has completed
making the fetch for getting the dynamic link data.
Which finally brings us to the addOnSuccessListener call
where we attach a new on success listener
to that task that will be called if everything went according
to plan and the call succeeded.
In here is where we will add our link handling
code since we will have received the link successfully
and can start to some processing on it.
OK.
Now let's get back to completing our on success listener.
So when I've successfully received the Dynamic Link,
let me first just log that to make sure I know,
hey, we've got our link.
So I'll add in a call here like so.
Log.I from my main activity and let me log the message,
we have a Dynamic Link.
Great.
Now that we've got our link, let's
extract the deep link URL.
So I'll do that by creating a variable handle
for the deep link URI object, and do
a null check on my pending Dynamic Link data
just to be safe.
And then extract the deep link data to my URI variable.
Nice.
OK, and now that we have our deep link data,
let's just log that to so that we know we've extracted it.
So, if deep link not equal a null.
Log.I my main activity.
Here is the deep link URL, and I'll
display what the deep link URL is that we've captured.
And finally, I'll add some links handling
code specific to my sample app.
In this case, I'll be setting the ViewPager
to a specific page of my app based on a current page
parameter denoted by curPage in my deep link URL.
And I'll send the user who clicked on this link
to the appropriate part of my app.
So I'll extract the URL link parameter
and capture that in my stream variable like so.
And after getting the current page,
I'll set my ViewPager appropriately.
This part might not be as important for you to note here,
as you'll want to modify this to fit whatever
makes sense for your app logic.
And of course we'll also need to add a failure listener in case
there are any errors retrieving that Dynamic Link data.
So I'll add that here as well and just log
the fact that we couldn't retrieve the Dynamic Link data.
So I'll addOnFailureListener and create
this new anonymous failure listener here.
And implement its on failure method
and then log the message from my main activity.
Whoops, couldn't retrieve Dynamic Link data.
OK, now we'll also want to include the same call
in our onStart method for activity.
So let me refactor what I'm doing here in a method called
checkForDynamicLinks.
So I can call it both from onCreate and onStart.
And now I'll make sure I'm calling them
in both those methods.
And there we have it.
We're now able to receive and interpret a Dynamic Link
in our Android app.
Actually you know what?
Let's test it out.
So let me bring up my handy app that literally just displays
a clickable Dynamic Link I've previously configured
in the Firebase console here.
And I'll click on the link and let's see what happens.
OK.
So on our device, my Learn Miwok app has opened up as expected.
And if I look in the console output,
I can see that, yes, we did receive the Dynamic Link.
And double yes, we were able to extract the deep link
URL from the Dynamic Link data.
But you know what?
I'm not satisfied yet.
I want to see this working where my app isn't already installed.
So let's try that.
So I'm going to delete my app from my phone
and reopen my handy app that displays my clickable Dynamic
Link and click it.
OK, so I'm taken to the Play Store.
Great.
I'm downloading my app.
Good.
It's installed.
Now I'll launch it, and, yes!
The app is launched correctly.
And now let's see in the console that we
received our Firebase Dynamic Link and Dynamic Link data?
And we did, oh, yeah, rock and roll, you all.
And there you have it.
You are now receiving Dynamic Links in your Android app.
Congratulations.
Oh, but I mentioned I would talk about using Firebase Dynamic
Links with app links, didn't I?
Yeah, let's talk about that.
Now one important thing to note is
that even if you do have existing app links,
you don't necessarily need to reuse them.
It is perfectly fine to use a new set of deep link URLs
specifically for your Firebase Dynamic Links.
And you'd receive and handle them just as we've shown.
But maybe you do want to reuse your app links because, hey,
whether your app is already installed,
and the user gets your app via an app link,
or if they come to your app via a Firebase Dynamic Link,
maybe the same flow can work for both paths into your app.
And if they do, then yes, you can definitely
reuse the existing app links you have configured.
To do that, let's rewind the tapes
and see what we need to change.
Remember, when we were adding intent filters to our Android
Manifest file?
Well, if you're using app links, you
will already have intent filters configured for matching URLs
to the appropriate activities.
So just leave those in there.
No need to add anything new.
You will need to use deep link URL
parameters that match the URL patterns for your app links
though.
So let's set that up now.
In part one of this series, we created a Dynamic Link
via the Firebase console.
We'll do that again here.
But let me now edit this Firebase Dynamic Link
by changing this deep link parameter to something
that will match the URL patterns for my existing app links.
There.
That does the trick.
And with that done, we use the same link
receiving and handling code that we set up for our Firebase
Dynamic Links that we showed earlier in each activity that
will match the deep link URL patterns we've
configured in our app links.
Yes.
Note that when a user already has the app installed
and clicks on a regular domain link directly,
they will go through the standard app link flow
and you will use the intent.getData
method you're already using to retrieve the link data.
For the Dynamic Link entry point use case
though, you'll need to use the Firebase Dynamic Links
SDK calls we showed earlier to receive and extract
the linked data instead.
Make sense?
Yeah.
And finally, what if you want to configure your Firebase Dynamic
Link as an app link?
That is, you want to capture clicks on a Firebase Dynamic
Link directly as an app link within your app
if the user already has it installed.
This can be useful to do if you want to avoid extra network
round trips.
And you still get the Firebase Dynamic Links analytics
data recorded even if you configure it as an app link.
So how do you do it?
It's actually pretty simple.
Let's go back to our Android Manifest file.
So first instead, of setting our Android host
to the URL matching the previous deep link parameter.
We'll set it to match the domain of our Firebase Dynamic Link.
That's right, the .page.link subdomain we created in part
one in the series when we created our first Dynamic Link.
This means that we will be receiving clicks
to Firebase Dynamic Links directly to our app
if it's already installed, rather than go
through the additional round trips to our Firebase servers.
Now note that you'll need to add this Android auto verify
equals true attribute to your intent filter
to make sure this is a verified link
and that a properly configured assetlinks.json
file is available on the host domain we've specified below.
This is the same auto generated as at assetlinks.json file that
was created when we made our first Dynamic Link in part one
in the series.
So don't worry.
This file is already in place.
And that's it.
To receive the Dynamic Link data,
you'll still need to make the Firebase Dynamic Links SDK
calls as we did earlier, that is the
FirebaseDynamicLinks.getInstance .getDynamicLink method calls
that we put earlier.
And that's because we may still need
to convert a short link into the expanded URL.
And we also want to track the analytics data
for this Firebase Dynamic Links click event.
Also note, that although the link data may also
exist in the Intent.getData method call
that you're using for app links, since we're
coming from a Firebase Dynamic Link in this case,
the captured linked data available in the getDynamicLink
getIntent call takes precedence, since the link
will be captured there.
And that is it.
You are rocking and rolling with Firebase Dynamic Links,
creating them and receiving them in your app.
Congratulations.
But wait.
There's more.
Remember how in my first video, I
mentioned that if you wanted to use custom
domains for your Firebase Dynamic Links,
you totally could do so?
Well, the next video in the series
is going to cover just that, using Firebase Dynamic Links
with your own custom domains.
If you want to see that, just stay tuned.
That'll be coming up soon.
Oh, and do you like Firebase videos?
Well if you want to see more of these,
hit that Subscribe button down there
and you got to see all of them as they come out.
Toodle-oo for now.