Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Dynamic Link is not caught by getInitialLink if app is closed and opened by that link

Programmatically generated dynamic links are not properly catched by

FirebaseDynamicLinks.instance.getInitialLink().

if the app is closed. However, if the app is open it is properly detected by the listener for new incoming dynamic links. It is not clear to me if it is a setup problem, how I generate the dynamic link.

To Reproduce

First set up Firebase for Flutter project as documented. Then to set up a dynamic link:

/// See also
/// https://firebase.google.com/docs/dynamic-links/use-cases/rewarded-referral
/// how to implement referral schemes using Firebase.
Future<ShortDynamicLink> buildDynamicLink(String userId) async {
  final PackageInfo packageInfo = await PackageInfo.fromPlatform();
  final String packageName = packageInfo.packageName;

  var androidParams = AndroidParameters(
    packageName: packageInfo.packageName,
    minimumVersion: Constants.androidVersion, // app version and not the Android OS version
  );

  var iosParams = IosParameters(
    bundleId: packageInfo.packageName,
    minimumVersion: Constants.iosVersion, // app version and not the iOS version
    appStoreId: Constants.iosAppStoreId,
  );

  var socialMetaTagParams = SocialMetaTagParameters(
    title: 'Referral Link',
    description: 'Referred app signup',
  );

  var dynamicLinkParams = DynamicLinkParameters(
    uriPrefix: 'https://xxxxxx.page.link',
    link: Uri.parse('https://www.xxxxxxxxx${Constants.referralLinkPath}?${Constants.referralLinkParam}=$userId'),
    androidParameters: androidParams,
    iosParameters: iosParams,
    socialMetaTagParameters: socialMetaTagParams,
  );

  return dynamicLinkParams.buildShortLink();
}

This dynamic link then can be shared with other new users.

I listen for initial links at app startup and then for new incoming links.

1) The link properly opens the app if the app is not running but the getInitialLink does not get it.

2) If the app is open the link is properly caught by the listener and all works.

Here is the very simple main.dart that I used to verify 1) that the initial link is not found with FirebaseDynamicLinks.instance.getInitialLink().

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  PendingDynamicLinkData linkData = await FirebaseDynamicLinks.instance.getInitialLink();
  String link = linkData?.link.toString();
  runApp(MyTestApp(link: link));
}

class MyTestApp extends StatelessWidget {
  final String link;

  MyTestApp({this.link});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        builder: (BuildContext context, Widget child) {
          return Scaffold(
            body: Container(
              child: Center(
                  child: Text('Initial dynamic Firebase link: $link')
              ),
            ),
          );
        }
    );
  }
}

Expected behavior

The link should open the app and trigger FirebaseDynamicLinks.instance.getInitialLink()..

Additional context

I hope properly configured Firebase project with Firebase console. To verify this I created a dynamic link to be used with Firebase Auth 'signup by email link' and these dynamic links are working as expected, also when the app is not open.

The point here is that the referral dynamic link that I generate programmatically is opening the app when it is closed but is then not caught by FirebaseDynamicLinks.instance.getInitialLink(), and to make things more confusing, works as expected if the app is open. In that case it is caught by the listener FirebaseDynamicLinks.instance.onLink.

I also set up the WidgetsBindingObserver in Flutter to handle that callback as required, when the app gets its focus back.

Any help is greatly appreciated. Debugging is very tricky, as you need to do it on a real device and not in the simulator. To make things worse, I did not figure out how to attach a debugger while the dynamic link opens the app. This means I am also stuck in investigating this issue further.

like image 782
Daniel Avatar asked Jan 21 '20 09:01

Daniel


Video Answer


1 Answers

  • In The FirebaseDynamicLinks Two Methods 1) getInitialLink() 2) onLink().

  • If When Your App Is Open And You Click On Dynamic Link Then Will Be Call FirebaseDynamicLinks.instance.onLink(), If Your App Is Killed Or Open From PlayStore Then You Get From FirebaseDynamicLinks.instance.getInitialLink();.

  • First Of You Need To Initialise Instance Of FirebaseDynamicLinks.instance.

      static void initDynamicLinks() async {
        final PendingDynamicLinkData data =
            await FirebaseDynamicLinks.instance.getInitialLink();
        final Uri deepLink = data?.link;
    
        if (deepLink != null && deepLink.queryParameters != null) {
          SharedPrefs.setValue("param", deepLink.queryParameters["param"]);
        }
    
        FirebaseDynamicLinks.instance.onLink(
            onSuccess: (PendingDynamicLinkData dynamicLink) async {
          final Uri deepLink = dynamicLink?.link;
    
          if (deepLink != null && deepLink.queryParameters != null) {
            SharedPrefs.setValue("param", deepLink.queryParameters["param]);
          }
        }, onError: (OnLinkErrorException e) async {
          print(e.message);
        });
      }
    
like image 169
Rohit Soni Avatar answered Oct 03 '22 04:10

Rohit Soni