Flutter Working with — DynamicLinking
Hello all,
Its been long since my last article, here today we deal with Dynamic Linking and generating short url’s for it.
Let’s get started,
First things first, We set up our firebase project for dynamic linking
If you have already done setting up your project for Dynamic Links you can skip this.
Visit https://console.firebase.google.com then click on your project
Now at left site in Grow section click on Dynamic Links then click on Get Started
Once you click on Get Started you will see the below screen
Here the screen says
Dynamic Links are created on the page.link domain. You may customize the link with your brand or app name. Customized links look more contextual and result in higher engagement.
Now enter your domain here I will select linkholder
Once you click continue you will see below text and click Finish
linkholder.page.link has been verified and approved for use
Now click on 3 dot menu on right side and select > Whitelist URL pattern
Now enter your domain as shown in the example pattern
^https://example\.com/.*$^https://play\.google\.com/.*id=myapp\.com$^https://itunes\.apple\.com/.*id123$^https://linkholder.page\.link/.*$^https://linkholder.page.link/.*$
like above form and click Add
Now we are good to go for App :)
Adding Dynamic Linking in App
First thing add dependencies
Note:
if you are using androidX artifact then go with
0.2.0
or else0.1.0+1
dependencies:
firebase_dynamic_links: ^0.2.0
For a simple use
Now we have done with our Dynamic Link generator methods now let’s generate a link and use it to get deep in our app.
To generate a short link
//import our newly created file into this file dynamic_linking.dartDynamicLinks.createPollLink(widget.poll.uid).then((uri) {
// Implement your shrare code here
// ...
});
Above code will generate a Dynamic Link and short it.
For detailed generating see the below code
final DynamicLinkParameters parameters = DynamicLinkParameters(
domain: 'abc123.app.goo.gl',
link: Uri.parse('https://example.com/'),
androidParameters: AndroidParameters(
packageName: 'com.example.android',
minimumVersion: 125,
),
iosParameters: IosParameters(
bundleId: 'com.example.ios',
minimumVersion: '1.0.1',
appStoreId: '123456789',
),
googleAnalyticsParameters: GoogleAnalyticsParameters(
campaign: 'example-promo',
medium: 'social',
source: 'orkut',
),
itunesConnectAnalyticsParameters: ItunesConnectAnalyticsParameters(
providerToken: '123456',
campaignToken: 'example-promo',
),
socialMetaTagParameters: SocialMetaTagParameters(
title: 'Example of a Dynamic Link',
description: 'This link works whether app is installed or not!',
),
);
final Uri dynamicUrl = await parameters.buildUrl();
To create a short Dynamic Link, build DynamicLinkParameters
the same way, but use the DynamicLinkParameters.buildShortLink()
method.
final ShortDynamicLink shortDynamicLink = await parameters.buildShortLink();
final Uri shortUrl = shortDynamicLink.shortUrl;
To shorten a long Dynamic Link, use the DynamicLinkParameters.shortenUrl method.
final ShortDynamicLink shortenedLink = await DynamicLinkParameters.shortenUrl(
Uri.parse(dynamicUrl),
DynamicLinkParametersOptions(ShortDynamicLinkPathLength.unguessable),
);final Uri shortUrl = shortenedLink.shortUrl;
Now to handle your received link
Handle received Link
Before we end up this article we need some important fixes to done inorder to work on iOS
Inside Info.plist file add the below code and don’t forget to change your URL Scheme com.example.app
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.example.app</string>
</array>
</dict>
</array>
Inside your Runner.entitlements don’t forget to change applinks:linkholder.page.link
To your link
<key>com.apple.developer.associated-domains</key>
<array>
<string>applinks:linkholder.page.link</string>
</array>
Hurray!! Now you are good to go. :)
Note:
If you are facing any crash in android while generating short URL refer my push request here https://github.com/flutter/plugins/pull/1142
**Thanks for your time.**
Hope you like it, if yes **clap & share.**