一尘不染

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

flutter

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

我想在Deeplink /动态链接中传递诸如用户名和密码之类的参数,如下所示:

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);
    });

仍检索到的数据为空。

有人可以帮忙吗?


阅读 502

收藏
2020-08-13

共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;
  }
}

你完成了。

2020-08-13