Flutter - How to Pass Custom Arguments in Firebase Dynamic Links for App Invite Feature

Flutter - How to pass custom arguments in firebase dynamic links for app invite feature?

The best way to use dynamic links are,

import 'package:firebase_dynamic_links/firebase_dynamic_links.dart';
import 'package:flutter/material.dart';

class TestPage extends StatefulWidget {
@override
_TestPageState createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {

@override
void initState() {
super.initState();
fetchLinkData();
}

void fetchLinkData() async {
// FirebaseDynamicLinks.getInitialLInk does a call to firebase to get us the real link because we have shortened it.
var link = await FirebaseDynamicLinks.instance.getInitialLink();

// This link may exist if the app was opened fresh so we'll want to handle it the same way onLink will.
handleLinkData(link);

// This will handle incoming links if the application is already opened
FirebaseDynamicLinks.instance.onLink(onSuccess: (PendingDynamicLinkData dynamicLink) async {
handleLinkData(dynamicLink);
});
}

void handleLinkData(PendingDynamicLinkData data) {
final Uri uri = data?.link;
if(uri != null) {
final queryParams = uri.queryParameters;
if(queryParams.length > 0) {
String userName = queryParams["username"];
// verify the username is parsed correctly
print("My users username is: $userName");
}
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Sample"),
),
body: Center(
child: Text("Test"),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
var dynamicLink = await createDynamicLink(userName: "Test");
// dynamicLink has been generated. share it with others to use it accordingly.
print("Dynamic Link: $dynamicLink");
},
child: Icon(
Icons.add,
color: Colors.white,
),
),
);
}

Future<Uri> createDynamicLink({@required String userName}) async {
final DynamicLinkParameters parameters = DynamicLinkParameters(
// This should match firebase but without the username query param
uriPrefix: 'https://test.page.link',
// This can be whatever you want for the uri, https://yourapp.com/groupinvite?username=$userName
link: Uri.parse('https://test.page.link/groupinvite?username=$userName'),
androidParameters: AndroidParameters(
packageName: 'com.test.demo',
minimumVersion: 1,
),
iosParameters: IosParameters(
bundleId: 'com.test.demo',
minimumVersion: '1',
appStoreId: '',
),
);
final link = await parameters.buildUrl();
final ShortDynamicLink shortenedLink = await DynamicLinkParameters.shortenUrl(
link,
DynamicLinkParametersOptions(shortDynamicLinkPathLength: ShortDynamicLinkPathLength.unguessable),
);
return shortenedLink.shortUrl;
}
}

You're done.

Firebase dynamic link support custom parameters?

I don't think you can use the short url:
https://<my app>.app.goo.gl/Gk3m
unless you create one for each user, but you can use the long url:
https://<my app>.app.goo.gl/?link=https://example.com/?token=fccfc8bfa07643a1ca8015cbe74f5f17 ...(add other parameters as needed)
and set new token for each user.

I assume you generate the tokens automatically. In that case you can use this to shorten the links.

How do I pass Parameters in Dynamic Links?

I finally figured it out!

I was understanding the concept totally wrong here.

There are 4 ways as of now to create dynamic links.

1) Firebase Console
2) Manually
3) Rest API
4) Dynamic Link Builder API on iOS and Android

What I was doing wrong here is was, I created https://subdomain.example.com/product a dynamic link from firebase console and was testing it against a manually created link.

2nd method(Manually) is much more powerful is you need to link dynamic content from your website links.

https://your_subdomain.page.link/?link=your_deep_link&apn=package_name[&amv=minimum_version][&afl=fallback_link]

The above mentioned is the standard manual procedure for creating dynamic links.

Lets break down the above link so that it looks less scary:

  • https://your_subdomain.page.link ==> This is simply your subdomain you registered on firebase console. In our case it's https://subdomain.example.com

  • link=your_deep_link ==> your_deep_link is basically your deep link(the link you want to open with that exists on your server, it can contain all the parameters you need). In our case its https://example.com/view-product?id=56. But note that this link is to be embedded inside an url so it needs to be urlencoded first. Use any url encoder for this purpose. The resulting encoded string becomes

https%3A%2F%2Fexample.com%2Fview-product%3Fid%3D56

  • apn=package_name ==> your respective package name for IOS or Android

  • [&amv=minimum_version] ==> "[]" represent this as an optional parameters. This parameter is the minimum version number of your app that you want your app should respond to this dynamic link (0 if you want all versions to support)

  • [&afl=fallback_link] ==> ==> "[]" represent this as an optional parameters. This is the fallback url, again url encoded. Could be your android play store link.

So our final dynamic link looks like:

https://subdomain.example.com/?link=https%3A%2F%2Fexample.com%2Fview-product%3Fid%3D56&apn=com.example&amv=0



Related Topics



Leave a reply



Submit