I may have almost forgotten about my promise in my first Dynamic Links post
I never did
Until I started working with Dynamic Links again for one of my own upcoming apps where I’m using email-invites as a way for people to collaborate on a single shopping list. If you know Dynamic Links, you know the drill. Link brings user to the app, link carries data with it, if user doesn’t have app, user is taken to Play Store for installation and link’s data is retained through the installation process.
Now if you’re using Dynamic Links to pass data to other users like I am, you know the Firebase Console builder isn’t going to cut it. Data is dynamic (or your app is garbage), and we need more flexibility.
In comes the Android Builder API for Dynamic Links where everything is set up in app. You can build your dynamic link in your app and pass it to an intent that prepares an email.
Add the Dependencies
implementation 'com.google.firebase:firebase-dynamic-links:16.1.3'
If you haven’t already, add this to your app-level build.gradle file.
Build the Dynamic Link
private fun buildDynamicLink(): String { return FirebaseDynamicLinks.getInstance().createDynamicLink() .setLink(Uri.parse("https://ericthecoder.com/invitation?listid=${listService.listID}")) .setDomainUriPrefix("https://shopshopshoppinglist.page.link") .setAndroidParameters(DynamicLink.AndroidParameters.Builder().build()) .buildDynamicLink() .uri .toString() }
Here’s a simple function I made to build the dynamic link I need. The data I’m passing along is contained in the link (listid). Of course, I’m using my own website to be the middle man between Firebase and my app. The DomainUriPrefix is taken from the Firebase Console when I set up Dynamic Links there. If you haven’t done that, head there now.
Assigning the Opened Activity
<activity android:name=".view.ListMembersActivity" > <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> <data android:host="ericthecoder.com" android:scheme="http"/> <data android:host="ericthecoder.com" android:scheme="https"/> </intent-filter> </activity>
This is all done in the Android Manifest. This whole intent filter is what shouts out to the dynamic link when it is opened.
Receiving the Dynamic Link
private fun receiveDynamicLink() { FirebaseDynamicLinks.getInstance().getDynamicLink(intent) .addOnCompleteListener { // Return if no dynamic link if (!it.isSuccessful || it.result == null) { Log.d(LOG_TAG, "No Dynamic Link Received") return@addOnCompleteListener } // Get the dyanamic link val dynamicLink = it.result!!.link val query = dynamicLink.getQueryParameter("listid") Log.d(LOG_TAG, "Received dynamic link $dynamicLink") Log.d(LOG_TAG, "Queried from dynamic link: $query") } }
In your chosen activity, you can steal the dynamic link from the intent that started the activity. After getting the dynamic link from the result, it’s all a matter of parsing the given URI to extract your data.
And that’s it
The Builder API is actually ridiculously simple. Somehow, the official docs still manages to split this whole process into 2 full pages. Sure there are a few things that I left out like buildShortDynamicLink and Analytics but come on.
If you guys in the Firebase team are reading this, how about bringing your boy in to handle the docs *wink wink nudge nudge*.
In any case, I don’t know how long it’s going to be til I get on with the other methods of creating Dynamic Links, if I even write about them at all. Here’s the thing. If you want it, or anything else for that matter, let me know in the comments.