Introduction to Firebase Dynamic Links
Firebase Dynamic Links are smart URLs that enable you to send existing and potential users to any location within your iOS or Android app. They survive the app install process, so even new users can see the content they were initially directed to.
Key Features of Firebase Dynamic Links:
- Deep Linking: Direct users to specific content within your app.
- Cross-Platform: Works on iOS, Android, and web.
- Survive App Install: Links retain their context even if the app is not installed.
- Customizable: Create short or long links with custom parameters.
Setting Up Firebase Dynamic Links
Step-by-Step Guide:
-
Enable Dynamic Links in Firebase Console:
- Go to the Firebase Console.
- Select your project.
- In the left-hand menu, navigate to "Engage" and select "Dynamic Links".
- Click "Get Started" and follow the prompts to enable Dynamic Links.
-
Add Firebase SDK to Your App:
- For Android:
dependencies { implementation 'com.google.firebase:firebase-dynamic-links:19.1.0' }
- For iOS:
pod 'Firebase/DynamicLinks'
- For Android:
-
Configure Your App:
- Android: Add the Dynamic Links intent filter to your
AndroidManifest.xml
:<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="example.page.link" android:scheme="https"/> </intent-filter>
- iOS: Configure your app to handle Universal Links by adding an
applinks
entry to yourAssociated Domains
in Xcode.
- Android: Add the Dynamic Links intent filter to your
Creating Dynamic Links
Using Firebase Console:
- Navigate to the Dynamic Links section in the Firebase Console.
- Click on "New Dynamic Link".
- Fill in the required fields:
- Link Name: A name for your link.
- Dynamic Link Domain: The domain you set up.
- Deep Link URL: The URL you want to deep link to.
- iOS and Android Behavior: Specify how the link should behave on each platform.
- Click "Create".
Programmatically:
-
Android:
FirebaseDynamicLinks.getInstance().createDynamicLink() .setLink(Uri.parse("https://www.example.com/")) .setDomainUriPrefix("https://example.page.link") .setAndroidParameters(new DynamicLink.AndroidParameters.Builder().build()) .setIosParameters(new DynamicLink.IosParameters.Builder("com.example.ios").build()) .buildShortDynamicLink() .addOnCompleteListener(new OnCompleteListener<ShortDynamicLink>() { @Override public void onComplete(@NonNull Task<ShortDynamicLink> task) { if (task.isSuccessful()) { Uri shortLink = task.getResult().getShortLink(); Uri flowchartLink = task.getResult().getPreviewLink(); // Use the shortLink } else { // Handle error } } });
-
iOS:
let link = URL(string: "https://www.example.com/")! let dynamicLinksDomainURIPrefix = "https://example.page.link" let builder = DynamicLinkComponents(link: link, domainURIPrefix: dynamicLinksDomainURIPrefix) builder?.iOSParameters = DynamicLinkIOSParameters(bundleID: "com.example.ios") builder?.androidParameters = DynamicLinkAndroidParameters(packageName: "com.example.android") builder?.shorten { (shortURL, warnings, error) in if let error = error { print("Error: \\(error.localizedDescription)") return } if let shortURL = shortURL { print("The short URL is: \\(shortURL)") } }
Handling Dynamic Links
Android:
- Add the following code to your
MainActivity
to handle the incoming dynamic link:@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FirebaseDynamicLinks.getInstance() .getDynamicLink(getIntent()) .addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() { @Override public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) { Uri deepLink = null; if (pendingDynamicLinkData != null) { deepLink = pendingDynamicLinkData.getLink(); } // Handle the deep link } }) .addOnFailureListener(this, new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.w(TAG, "getDynamicLink:onFailure", e); } }); }
iOS:
- Add the following code to your
AppDelegate
to handle the incoming dynamic link:func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool { guard let incomingURL = userActivity.webpageURL else { return false } let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamiclink, error) in guard error == nil else { print("Error: \\(error!.localizedDescription)") return } if let dynamicLink = dynamiclink, let url = dynamicLink.url { // Handle the deep link } } return linkHandled }
Practical Exercise
Task:
Create a Firebase Dynamic Link that directs users to a specific page in your app. Ensure the link works on both Android and iOS platforms.
Steps:
- Set up Firebase Dynamic Links in the Firebase Console.
- Add the necessary dependencies and configurations to your Android and iOS projects.
- Create a dynamic link programmatically.
- Handle the dynamic link in your app to navigate to the specified page.
Solution:
Follow the steps outlined in the "Setting Up Firebase Dynamic Links" and "Creating Dynamic Links" sections above. Ensure you test the link on both platforms to verify it works as expected.
Common Mistakes and Tips
- Incorrect Domain Configuration: Ensure your dynamic link domain is correctly set up in the Firebase Console and matches the domain used in your app configuration.
- Handling Links: Always check for null values when handling incoming dynamic links to avoid crashes.
- Testing: Test your dynamic links thoroughly on both iOS and Android to ensure they work as expected.
Conclusion
Firebase Dynamic Links provide a powerful way to enhance user experience by directing users to specific content within your app, even if the app is not installed. By following the steps outlined in this module, you can set up, create, and handle dynamic links effectively in your Android and iOS applications.
Firebase Course
Module 1: Introduction to Firebase
Module 2: Firebase Authentication
- Introduction to Firebase Authentication
- Email and Password Authentication
- Social Media Authentication
- Managing Users
Module 3: Firebase Realtime Database
- Introduction to Realtime Database
- Reading and Writing Data
- Data Structure and Security Rules
- Offline Capabilities
Module 4: Cloud Firestore
- Introduction to Cloud Firestore
- Firestore Data Model
- CRUD Operations
- Advanced Queries
- Security Rules
Module 5: Firebase Storage
Module 6: Firebase Cloud Messaging
- Introduction to Cloud Messaging
- Sending Notifications
- Handling Notifications
- Advanced Messaging Features