Flutter - 如何在应用邀请功能的 Firebase 动态链接中传递自定义参数?

Posted

技术标签:

【中文标题】Flutter - 如何在应用邀请功能的 Firebase 动态链接中传递自定义参数?【英文标题】:Flutter - How to pass custom arguments in firebase dynamic links for app invite feature? 【发布时间】:2020-02-17 06:43:12 【问题描述】:

您能告诉我如何使用颤振插件 firebase_dynamic_links[https://pub.dev/packages/firebase_dynamic_links#-readme-tab-] 发送和获取参数吗?

我想在深层链接/动态链接中传递用户名和密码等参数,如下所示:

uriPrefix: 'https://test.page.link/appinvite?username=Test&password=123456',
link: Uri.parse('https://test.page.link/appinvite?username=Test&password=123456'),

这是传递数据的正确方法吗?

之后我使用下面的代码来获取数据,

await FirebaseDynamicLinks.instance.getInitialLink();
final Uri deepLink = data?.link;

但是,它为我提供了我在 Firebase 控制台中添加的虚拟 Web URL,用于 Deep Link URL 中的 Firebase 动态链接

目前我可以从 firebase 动态链接打开应用,但无法获取自定义参数。

任何帮助将不胜感激。

更新代码:

发送邀请:

_generateAndShareDynamicLink() async 
    final DynamicLinkParameters parameters = DynamicLinkParameters(
      uriPrefix: 'https://test.page.link/groupinvite',
      link: Uri.parse('https://test.page.link/groupinvite'),
      androidParameters: AndroidParameters(
        packageName: 'com.test.flutter_authentication',
        minimumVersion: 0,
      ),
      dynamicLinkParametersOptions: DynamicLinkParametersOptions(
        shortDynamicLinkPathLength: ShortDynamicLinkPathLength.unguessable,
      ),
    );

    Uri url = await parameters.buildUrl();

    shareURL(Uri.https(url.authority, url.path, "username": "Test"));

用于在 initState() 中获取数据:

FirebaseDynamicLinks.instance.onLink(
        onSuccess: (PendingDynamicLinkData dynamicLink) async 
          final Uri deepLink = dynamicLink?.link;
          _scaffoldKey.currentState.showSnackBar(SnackBar(content: Text("deepLink2 : $deepLink",)));
          if (deepLink != null) 
            Map sharedListId = deepLink.queryParameters;
            print("sharedListId : $sharedListId");

            String username=sharedListId["username"];
            print("username : $username");

            Navigator.pushNamed(context, deepLink.path);
          
        , onError: (OnLinkErrorException e) async 
      print('onLinkError');
      print(e.message);
    );

仍然检索到的数据为空。

有人可以帮忙吗?

【问题讨论】:

【参考方案1】:

使用动态链接的最佳方式是,

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;
  

你已经完成了。

【讨论】:

我用同样的方法创建了链接。但是生成的链接说在浏览器上打开时找不到动态链接。而且此处创建的链接也不会显示在 Firebase 控制台中。 脱帽致敬先生。一直在寻找给链接加参数,不满意。这对我有很大帮助。谢谢 URI 在 URL 内部显示的 Web 浏览器参数重定向期间工作正常,但我没有得到 queryParameters。 @abhijat_saxena 我很高兴能帮上忙。起初,当我的要求没有答案时,我也有同样的感觉。 添加评论以帮助我将来找到此解决方案。谢谢杰【参考方案2】:

这对我有用

发送方:

Future<String> generateLink() async 
final DynamicLinkParameters parameters = DynamicLinkParameters(
      uriPrefix: 'https://<your-domain-name>.page.link',
      link: Uri.parse(
          'https://<your-domain-name>.page.link/<your-route>/?id=acb&name=me'), // <- your paramaters
      dynamicLinkParametersOptions: DynamicLinkParametersOptions(
          shortDynamicLinkPathLength: ShortDynamicLinkPathLength.unguessable),
      androidParameters: AndroidParameters(
        packageName: '<package-name>',
        minimumVersion: 0,
      ),
      socialMetaTagParameters: SocialMetaTagParameters(
        title: "click the link",
      ),
    );
    final Uri dynamicUrl = await parameters.buildUrl();
    final ShortDynamicLink shortenedLink =
        await DynamicLinkParameters.shortenUrl(
      dynamicUrl,
      DynamicLinkParametersOptions(
          shortDynamicLinkPathLength: ShortDynamicLinkPathLength.unguessable),
    );
    final Uri shortUrl = shortenedLink.shortUrl;
    return "https://<your-domain-name>.page.link" + shortUrl.path;
  

接收方:

FirebaseDynamicLinks.instance.onLink(
    onSuccess: (PendingDynamicLinkData dynamicLink) async 
      final Uri deepLink = dynamicLink?.link;
      if (deepLink != null) 
        print(deepLink.queryParameters['id']); // <- prints 'abc'
      
    , onError: (OnLinkErrorException e) async 
    print('onLinkError');
    print(e.message);
  
);

【讨论】:

以上是关于Flutter - 如何在应用邀请功能的 Firebase 动态链接中传递自定义参数?的主要内容,如果未能解决你的问题,请参考以下文章

我可以在使用 AWS amplify for Cognito 和 DataStore 的同时使用 fire base 来托管我的 Flutter Web 应用程序吗

如何在flutter中实现agora视频通话邀请

如何通过使用flutter bloc从fire存储中使用依赖注入来处理错误`The getter was called on null`

活动邀请 | Flutter Live Viewing Party

firebase、flutter应该如何处理好友请求和邀请? [关闭]

Flutter:Fire Base 存储图像上传的后期初始化错误