Add query parameters to link in firebase dynamic link
Asked Answered
H

4

6

I create dynamic link and I want to send some specific parameter, like: "https://mydynamiclink/?link=" + link + "&msgid=" + id + "&apn=myapn". link field looks like "https://play.google.com/store/apps/details/?id=com.myApp&msgid=myId&apn=myapn"

When I open my app after taping on this link - I receive PendingDynamicLinkData and can get link from it, but not some custom data. (pendingDynamicLinkData.getLink() returns my link without "&msgid=..." - I'm getting string "https://play.google.com/store/apps/details/?id=com.myApp")

How can I add my msgid field and get it after all?

Hopefully answered 23/10, 2018 at 7:36 Comment(0)
H
4

I've found solution

String query = "";
try {
    query = URLEncoder.encode(String.format("&%1s=%2s", "msgid", id), "UTF-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

final String link = "https://play.google.com/store/apps/details/?id=com.myApp" + query;

After such encoding pendingDynamicLinkData.getLink() returns me https://play.google.com/store/apps/details/?id=com.myApp&msgid=myId

Hopefully answered 30/10, 2018 at 7:55 Comment(2)
This is my solution for sending/receiving parameters using long url https://mcmap.net/q/1017713/-add-parameter-to-firebase-dynamic-linksStrenuous
As I've written, pendingDynamicLinkData.getLink() doesn't work for me.Hopefully
G
5

Accepted answer didn't work out fine for me, all i needed to do was check if the link was for a user's profile and not a blog post, so i can redirect to my ProfileActivity instead.

private void generateDynamicLink() {
    //build link normally and add queries like a normal href link would
    String permLink = getLink() + "?route=profile&name=" + getProfileName()
            + "&category=" + getUserPracticeCategory()
            + "&picture=" + getProfilePicture();

    FirebaseDynamicLinks.getInstance().createDynamicLink()
            .setLink(Uri.parse(permLink))
            .setDynamicLinkDomain(Constants.DYNAMIC_LINK_DOMAIN)
            .setAndroidParameters(new 
    DynamicLink.AndroidParameters.Builder().build())
            .setSocialMetaTagParameters(
                    new DynamicLink.SocialMetaTagParameters.Builder()
                            .setTitle("Enter Title")
                            .setDescription("Enter Desc here")
                            .setImageUrl(Uri.parse(getProfilePicture()))
                            .build())
            .buildShortDynamicLink()
            .addOnCompleteListener(this, task -> {
                if (task.isSuccessful()) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_SEND);
                    intent.putExtra(Intent.EXTRA_TEXT,task.getResult().getShortLink());
                    intent.setType("text/plain");
                    startActivity(intent);
                } else {
                    Utils.snackBar(tvAddress, "Failed to Generate Profile Link, Try 
Again");
                }
            });
}

and when a user navigates into my app using the generated link, it goes to a post detail activity, because i made that activity the only browsable activity in my manifest. i then have to use the route query to determine if the incoming link is a blog post or a shared user profile.

private void retrieveDynamicLink() {
    FirebaseDynamicLinks.getInstance().getDynamicLink(getIntent())
            .addOnSuccessListener(this, pendingDynamicLinkData -> {
                if (pendingDynamicLinkData == null) {
                    retrieveLocalIntent();
                } else {
                    Toast.makeText(context, "Resolving Link, Please Wait...", Toast.LENGTH_LONG).show();
                    if (pendingDynamicLinkData.getLink().getQueryParameter("route") != null) {
                        if (Objects.requireNonNull(pendingDynamicLinkData.getLink().getQueryParameter("route")).equalsIgnoreCase("profile")) {
                            try {
                                Uri uri = pendingDynamicLinkData.getLink();
                                String permLink = uri.toString().split("\\?")[0];
                                Intent intent = new Intent(this, ProfileActivity.class);
                                intent.putExtra(ProfileActivity.PROFILE_NAME, uri.getQueryParameter("name"));
                                intent.putExtra(ProfileActivity.PROFILE_CATEGORY, uri.getQueryParameter("category"));
                                intent.putExtra(ProfileActivity.PROFILE_PICTURE, uri.getQueryParameter("picture"));
                                intent.putExtra(Utils.POST_PERMLINK, permLink);
                                startActivity(intent);
                                this.finish();
                            } catch (NullPointerException e) {
                                Toast.makeText(context, "Unable to View User Profile", Toast.LENGTH_SHORT).show();
                            }
                        }
                    } else {
                        postHrefLink = pendingDynamicLinkData.getLink().toString();
                        getPostDetail.getData(postHrefLink);
                    }
                }
            })
            .addOnFailureListener(this, e ->
                    retrieveLocalIntent()
            );
}

Hope this helps.

Gillenwater answered 10/1, 2019 at 11:0 Comment(0)
H
4

I've found solution

String query = "";
try {
    query = URLEncoder.encode(String.format("&%1s=%2s", "msgid", id), "UTF-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

final String link = "https://play.google.com/store/apps/details/?id=com.myApp" + query;

After such encoding pendingDynamicLinkData.getLink() returns me https://play.google.com/store/apps/details/?id=com.myApp&msgid=myId

Hopefully answered 30/10, 2018 at 7:55 Comment(2)
This is my solution for sending/receiving parameters using long url https://mcmap.net/q/1017713/-add-parameter-to-firebase-dynamic-linksStrenuous
As I've written, pendingDynamicLinkData.getLink() doesn't work for me.Hopefully
R
0

1 First Change your Dynamic Link in firebase console from http://exampleandroid/test to http://exampleandroid/test?data 2. You send the query paramter data with this

 DynamicLink dynamicLink = FirebaseDynamicLinks.getInstance().createDynamicLink()
                   // .setLink(dynamicLinkUri)
                    .setLink(Uri.parse("http://exampleandroid/test?data=dsads"))
                    .setDomainUriPrefix("https://App_Name.page.link")
                    // Open links with this app on Android
                    .setAndroidParameters(new DynamicLink.AndroidParameters.Builder().build())
                    // Open links with com.example.ios on iOS
                    .setIosParameters(new DynamicLink.IosParameters.Builder("com.appinventiv.ios").build())
                    .buildDynamicLink();

            dynamicLinkUri = dynamicLink.getUri();
Ribaldry answered 30/1, 2019 at 10:20 Comment(0)
G
-1

Let's say that You want to create the following URL:

https://www.myawesomesite.com/turtles/types?type=1&sort=relevance#section-name

For this you can do following

 Uri.Builder builder = new Uri.Builder();
 builder.scheme("https")
.authority("www.myawesomesite.com")
.appendPath("turtles")
.appendPath("types")
.appendQueryParameter("type", "1")
.appendQueryParameter("sort", "relevance")
.fragment("section-name");

String myUrl = builder.build().toString();
Glutinous answered 23/10, 2018 at 7:48 Comment(1)
Question is not about building link. I have my link, I try to add it to firebase dynamic link. And when I get PendingDynamicLinkData from dynamic link I can retrieve my link from it. But my link without needed parameter (msgId)Hopefully

© 2022 - 2024 — McMap. All rights reserved.