通过对外分享的内容打开Flutter App

Open Flutter App through externally shared content

我的 flutter 应用程序有一个按钮,可以将内容分享到其他应用程序,例如 whatsapp、facebook 等。截至目前,它仅共享文本。我想这样做,以便当用户通过 Whatsapp 和其他应用程序共享内容时,接收者可以单击共享的 link、内容或其他内容,并且(如果他有应用程序)在导航到时启动应用程序与共享内容相关的页面。

如果我分享了特定车型,其他用户可以点击 whatsapp 中的内容,启动应用程序导航到包含所分享特定车型详细信息的 CarPage。

我现在正在使用这个包 https://pub.dev/packages/share,它允许 ACTION_SEND 意图。

你需要firebase_dynamic_links

If a user opens a Dynamic Link on iOS or Android, they can be taken directly to the linked content in your native app.

如何创建动态link?

You create a Dynamic Link either by using the Firebase console, using a REST API, iOS or Android Builder API, Flutter API, or by forming a URL by adding Dynamic Link parameters to a URI prefix specific to your app.

final DynamicLinkParameters parameters = DynamicLinkParameters(
  uriPrefix: 'https://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();
// use this code to sorten the url
final ShortDynamicLink shortenedLink = await DynamicLinkParameters.shortenUrl(
  Uri.parse('https://example.page.link/?link=https://example.com/&apn=com.example.android&ibn=com.example.ios'),
  DynamicLinkParametersOptions(ShortDynamicLinkPathLength.unguessable),
);

final Uri shortUrl = shortenedLink.shortUrl;

接收动态link

 void initDynamicLinks() async {
    final PendingDynamicLinkData data = await FirebaseDynamicLinks.instance.getInitialLink();
    final Uri deepLink = data?.link;

    if (deepLink != null) {
      Navigator.pushNamed(context, deepLink.path);
    }

    FirebaseDynamicLinks.instance.onLink(
      onSuccess: (PendingDynamicLinkData dynamicLink) async {
        final Uri deepLink = dynamicLink?.link;

        if (deepLink != null) {
          Navigator.pushNamed(context, deepLink.path);
        }
      },
      onError: (OnLinkErrorException e) async {
        print('onLinkError');
        print(e.message);
      }
    );
  }