简体   繁体   English

Flutter - Firebase 动态链接没有被onLink捕获但是在iOS上打开应用

[英]Flutter - Firebase Dynamic Link is not caught by onLink but open the app on iOS

Everything works fine on android but on ios when the app is already opened clicking the link takes the app in the foreground but the onLink method is not call.在 android 上一切正常,但在 ios 上,当应用程序已经打开时,单击链接会将应用程序置于前台,但不会调用onLink方法。

Link:关联:

https://<url>/?link=<link>&apn=<apn>&ibi=<bundle>&isi=<isi>

Package: Package:

  • firebase_dynamic_links: ^0.6.3 firebase_dynamic_links:^0.6.3

Code代码

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

class DynamicLinksService {

  Future handleDynamicLinks(BuildContext context) async {
    final PendingDynamicLinkData data =
        await FirebaseDynamicLinks.instance.getInitialLink();

    await _handleDynamicLink(context, data);

    FirebaseDynamicLinks.instance.onLink(
        onSuccess: (PendingDynamicLinkData dynamicLinkData) async {
      await _handleDynamicLink(context, dynamicLinkData);
    }, onError: (OnLinkErrorException e) async {
      print('Dynamic link failed: ${e.message}');
    });
  }

  Future _handleDynamicLink(
      BuildContext context, PendingDynamicLinkData data) async {
    final Uri deepLink = data?.link;
    if (deepLink != null) {
      print('_handleDeepLink | deepLink $deepLink');

      await _doSomething(context, deepLink.toString());
    } else {
      print('no deepLink');
    }
  }
}

From my experimentation, onLink is not called on iOS however you can call getInitialLink() and it will contain the link.根据我的实验,onLink 不会在 iOS 上调用,但是您可以调用 getInitialLink() 并且它将包含链接。 I'm uncertain if this is by design or a bug, but it seems to be across a few versions.我不确定这是设计使然还是错误,但它似乎跨越了几个版本。

Example service code:示例服务代码:

  Future<Uri> retrieveDynamicLink(BuildContext context) async {
    try {
      final PendingDynamicLinkData data = await FirebaseDynamicLinks.instance.getInitialLink();
      final Uri deepLink = data?.link;
      return deepLink;
    } catch (e) {
      print(e.toString());
    }

    return null;
  }

Widget snippet小部件片段

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }


  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
      if (state == AppLifecycleState.resumed){
        final _timerLink = Timer(
          const Duration(milliseconds: 1000),
              () async {
            final auth = Provider.of<FirebaseAuthService>(context, listen: false);
            final link = await auth.retrieveDynamicLink(context);
            _handleLink(link);
          },
        );
      }
  }

Make sure to add the WidgetsBindingObserver确保添加 WidgetsBindingObserver

class _SignInPageState extends State<SignInPage> with TickerProviderStateMixin, WidgetsBindingObserver{

I'm not sure why this works but you'd first have to call FirebaseMessaging.instance.getInitialMessage() at least once before your onLink callback is activated by Firebase.我不确定为什么会这样,但您首先必须至少调用FirebaseMessaging.instance.getInitialMessage()一次,然后您的onLink回调才会被 Firebase 激活。

Not sure if this is by design or a bug.不确定这是设计使然还是错误。

Let me know if this works.让我知道这个是否奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的 flutter iOS 应用程序尚未发布。 如果应用程序尚未安装,我如何知道我的 firebase 动态链接可以打开 Appstore? - My flutter iOS app hasn't been released yet. How can I know my firebase dynamic link works to open Appstore if the app isn't installed yet? Firebase 动态链接打不开app iOS - Firebase dynamic link not opening the app iOS Flutter iOS Firebase 带链接值的动态链接推送命名页面 - Flutter iOS Firebase dynamic link push named page with the link value Flutter - Firebase 当应用处于终止模式时动态链接不工作 - Flutter - Firebase Dynamic Link not Working while app is in kill mode Flutter 每次打开应用程序时都会触发动态链接 - Flutter Dynamic Link triggers everytime I open app Flutter firebase 动态链接不听 - Flutter firebase dynamic link not listening Firebase 动态链接在 IOS 上不起作用 - Firebase dynamic link is not working on IOS Flutter Firebase - 如何在点击 Firebase 发送的 email 验证链接时打开应用程序? - Flutter Firebase - How to open the app when tapping on email verification link send by Firebase? Firebase 动态链接打开特定活动 - Firebase Dynamic Link open specific Activity Flutter firebase 动态链接在应用程序上处理 url 时使用自定义参数自定义链接 - Flutter firebase dynamic links customise link with custom params when handling url on the app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM