Uncle
Uncle

Reputation: 71

Firebase Dynamic Links failing to fetch parameter

I am working on invites in my app, I want users to refer others, the referrer will get a reward after the person they referred registers. I was following this firebase invite tutorial, in step 2 we created a dynamic link from our normal link with our parameter invitedby=SENDER_UID like

           String link = "https://samplelink.com/?invitedby=" + user.getPhone();
        Log.e(TAG, "Generated link: "+link);
        FirebaseDynamicLinks.getInstance().createDynamicLink()
                .setLink(Uri.parse(link))
                .setDynamicLinkDomain(getString(R.string.dynamic_link_domain))
                .setAndroidParameters(
                        new DynamicLink.AndroidParameters.Builder(getPackageName())
                                .build())
                .setIosParameters(
                        new DynamicLink.IosParameters.Builder("com.example.ios")
                                //.setAppStoreId("123456789")
                              //  .setMinimumVersion("1.0.1")
                                .build())
                .buildShortDynamicLink()
                .addOnSuccessListener(shortDynamicLink -> {

                    Uri mInvitationUrl = shortDynamicLink.getShortLink();
                    Log.e(TAG, "generated URL: "+mInvitationUrl.toString());
                    Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invite_and_earn))
                            .setMessage(getString(R.string.invitation_message))
                            .setDeepLink(mInvitationUrl)
                            //   .setCustomImage(Uri.parse(getString(R.string.invitation_custom_image)))
                           // .setCallToActionText(user.getPhone())
                            .build();
                    startActivityForResult(intent, REQUEST_INVITE);
                }).addOnFailureListener(e -> Log.e(TAG, "Error Generating Deeplink: "+e.getLocalizedMessage()));


    });

And sent the generated short dynamic link. When the users opened the app in step 4 we got the link like

 FirebaseDynamicLinks.getInstance()
        .getDynamicLink(getIntent())
        .addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() {
            @Override
            public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {
                // Get deep link from result (may be null if no link is found)
                Uri deepLink = null;
                if (pendingDynamicLinkData != null) {
                    deepLink = pendingDynamicLinkData.getLink();
                }
                //
                // If the user isn't signed in and the pending Dynamic Link is
                // an invitation, sign in the user anonymously, and record the
                // referrer's UID.
                //
                FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                if (user == null
                        && deepLink != null
                        && deepLink.getBooleanQueryParameter("invitedby")) {
                    String referrerUid = deepLink.getQueryParameter("invitedby");
                    createAnonymousAccountWithReferrerInfo(referrerUid);
                }
            }
        });

The link does shorted dynamic link generate fine, and sent, if opened in the browser the original https://samplelink.com/?invitedby=" + user.getPhone() displayed but in the app I only get the shortdynamiclink so invitedby is never found. Anyone had this issue?

A sample of my short dynamic link

Upvotes: 3

Views: 1582

Answers (2)

Uncle
Uncle

Reputation: 71

I ended up using the Invite API instead

Uri uri = Uri.parse(getString(R.string.invitation_deep_link));
        Uri deepLinkPlus = Uri.withAppendedPath(uri, user.getPhone());
        Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invite_and_earn))
                .setMessage(getString(R.string.invitation_message))
                .setDeepLink(deepLinkPlus)
                //.setCustomImage(Uri.parse(getString(R.string.invitation_custom_image)))
                //.setCallToActionText("invitedby")
                .build();
        startActivityForResult(intent, REQUEST_INVITE);

And from the activity that receives the deeplink

 FirebaseDynamicLinks.getInstance().getDynamicLink(getIntent())
            .addOnSuccessListener(this, data -> {
                if (data == null) {
                    Log.d(TAG, "getInvitation: no data");
                    return;
                }
                Uri deepLink = data.getLink();
                String referrerPhone = deepLink.getLastPathSegment();
                SharedPreferencesUtils.saveString(this, SharedPreferencesUtils.PREF_REFERER, referrerPhone);

            })
            .addOnFailureListener(this, e -> Log.w(TAG, "getDynamicLink:onFailure", e));

Upvotes: 1

Rohit Maurya
Rohit Maurya

Reputation: 750

Instead of concatenating parameter with the link you can use appendQueryParameter()

String link = "https://samplelink.com/";
        Log.e(TAG, "Generated link: "+link);
        FirebaseDynamicLinks.getInstance().createDynamicLink()
                .setLink(Uri.parse(link).buildUpon().appendQueryParameter("invitedby",user.getPhone()).build())
                .setDynamicLinkDomain(getString(R.string.dynamic_link_domain))
                .setAndroidParameters(
                        new DynamicLink.AndroidParameters.Builder(getPackageName())
                                .build())
                .setIosParameters(
                        new DynamicLink.IosParameters.Builder("com.example.ios")
                                //.setAppStoreId("123456789")
                              //  .setMinimumVersion("1.0.1")
                                .build())

And in your activity on can retrieve your parameter as follow,

FirebaseDynamicLinks.getInstance().getDynamicLink(getActivity().getIntent())
                    .addOnSuccessListener(getActivity(), new OnSuccessListener<PendingDynamicLinkData>() {
                        @Override
                        public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {
                            Uri deepLink = null;
                            if (pendingDynamicLinkData != null) {
                                deepLink = pendingDynamicLinkData.getLink();
                            }
                            final Uri finalDeepLink = deepLink;

                            if (finalDeepLink != null && finalDeepLink.getBooleanQueryParameter("invitedby", false)) {
                                String referrerPhoneId = finalDeepLink.getQueryParameter("invitedby");

Upvotes: 1

Related Questions