Add parameter to Firebase Dynamic Links
Asked Answered
B

2

4

I want to let the user share a product from my app by clicking a button and sending other potential users links like

www.myapp.com/offer/123

there, "123" must be generated at the moment the user click the button in order to, later in time, hanle it with

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

but unfortunetly I am unable to pass a parameter.

String link = "http://www.myapp.com/offer/123";
        Task<ShortDynamicLink> shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
                .setLink(Uri.parse(link))
                .setDynamicLinkDomain("fgd3e.app.goo.gl")
                .buildShortDynamicLink()
                .addOnCompleteListener(this, new OnCompleteListener<ShortDynamicLink>() {
                    @Override
                    public void onComplete(@NonNull Task<ShortDynamicLink> task) {
                        if (task.isSuccessful()) {
                            // Short link created
                            Uri shortLink = task.getResult().getShortLink();
                            Uri flowchartLink = task.getResult().getPreviewLink();

Can someone teach me how to create a dynamic link at runtime with custom parameters in order to re direct the target user to specific product detail?

Bosson answered 13/4, 2018 at 23:1 Comment(0)
S
11

SHORT ANSWER: Using query parameters instead of path variables you could use the getQueryParameter method from the Uri object returned by pendingDynamicLinkData.getLink()


What i've been doing is using query parameters instead of path variables.

Instead of sending http://www.myapp.com/offer/123, i'm sending something like http://www.myapp.com/?offer=123

To add parameters dynamically i'm just concatenating strings: "http://www.myapp.com/?offer=" + myValue

This URL is in turn a query parameter of the dynamic link created in firebase:

String url = "https://YourDynamicLinkIdentifier.app.goo.gl/?link=https://myapp.com?offer=" 
+ myOfferVar 
+ "&apn=com.your.apn"; // << Dont forget to change this too

And this resulting URL is the one i send to the url shortener.

Then in the callback onSuccess(PendingDynamicLinkData pendingDynamicLinkData) call getLink() of pendingDynamicLinkData as you're already doing.

Now that you have a Uri object, you can easily get the parameter by calling the method getQueryParameter("offer").

if (pendingDynamicLinkData != null) {
     deepLink = pendingDynamicLinkData.getLink();
     String offerKey = deepLink.getQueryParameter("offer");

NOTE: In case you still prefer to use the path variable, you could get the last segment of the Uri path. See How to obtain the last path segment of an uri

Schiro answered 14/4, 2018 at 7:38 Comment(4)
And what do you have in the Dinamic Links part, in your Firebase Console? Did you created/configurated the dynamic link there??Bosson
Yes, i only created it, there's no need for any special configuration for parameters (in firebase) since the link you will be receiving in the app is the deeplink you provide plus all the parameters you add. There's something i didnt mention, let me update the answer.Streamliner
@Schiro So, if a User installed the Application using the dynamic Link through Playstore and then close the PlayStore and then open the App from the Menu then the dynamic data will be intact or not? Will I be able to detect the referrer? The extras won't get cleared if the Other user doesn't open the App from the PlayStore.Tayler
@Schiro Hi i still didn't understand with your answer, so what kind a dynamic link should I create on firebase console to achieve dynamic query parameters?Prophetic
M
5

You need to use long deep link to send parameters.

Example:

1) link for opening the app by google play testing url:

https://xx.page.link/?link=https://xx.com/invitation/?id=2&apn=com.xx.app&afl=https://play.google.com/apps/testing/com.xx.app

2) receving the parameter:

       FirebaseDynamicLinks.getInstance()
            .getDynamicLink(getIntent())
            .addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() {
                @Override
                public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {
                    Uri deepLink = null;
                    if (pendingDynamicLinkData != null) {
                        deepLink = pendingDynamicLinkData.getLink();
                        String paramValue = deepLink.getQueryParameters("id").get(0)); // it will get "2" as a value
                    }

                }
            })
            .addOnFailureListener(this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.w("Splash", "getDynamicLink:onFailure", e);
                }
            });
Mattson answered 21/12, 2019 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.