10

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.

Link:

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

Package:

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

Fabio Tredesini
  • 101
  • 1
  • 3
  • Did you add domain settings to the associated domain? – Chance Mar 02 '21 at 13:59
  • yes, my url registered on firebase is `dynamic.vendoa.it` and i've added the following `applinks:dynamic.vendoa.it` – Fabio Tredesini Mar 02 '21 at 14:10
  • did you get this working ? – epynic Mar 19 '21 at 11:30
  • 5
    I'm having this same issue. App opens, but onLink isn't called. Any update? – MousyBusiness May 21 '21 at 12:32
  • 1
    It worked for me! Basically, you need to add 2-3 seconds delay on initiating + you can handle the deeplink out of (onSuccess). It worked for me finally! Check this link below https://betterprogramming.pub/deep-linking-in-flutter-with-firebase-dynamic-links-8a4b1981e1eb – Ali Obeid Jun 25 '21 at 01:22

4 Answers4

1

For me, none of the suggestions solved the issue. Adding a delay for 3 seconds etc. I tried many things. At the end, it was all about url encoding. The link attribute on the dynamic link should be url encoded.

For example, this link doesn't work

https://your.page.link/?efr=0&ibi=your_ibi&isi=your_isi&apn=your_apn&link=https://example.com?param=value

But this one works

https://your.page.link/?efr=0&ibi=your_ibi&isi=your_isi&apn=your_apn&link=https%3A%2F%2Fexample.com%3Fparam%3Dvalue

A. Sahin
  • 51
  • 3
0

From my experimentation, onLink is not called on iOS however you can call getInitialLink() and it will contain the link. 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

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

MousyBusiness
  • 188
  • 2
  • 14
0

For me, the problem was that I was making up my own (valid) link for testing. Turns out correct format is not enough, it has to be a link generated by Firebase. Either through the portal or by code.

This is most probably not the fix for most people, but this solved it for me, and might save someone's time in the future.

Abdelrahman Sherif
  • 2,080
  • 1
  • 9
  • 12
-1

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.

Not sure if this is by design or a bug.

Let me know if this works.

mzaink
  • 261
  • 4
  • 10