Andrii Turkovskyi
Andrii Turkovskyi

Reputation: 29438

Add query parameters to link in firebase dynamic link

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?

Upvotes: 6

Views: 10336

Answers (4)

Dishant Kawatra
Dishant Kawatra

Reputation: 648

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();

Upvotes: 0

Peterstev Uremgba
Peterstev Uremgba

Reputation: 729

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.

Upvotes: 5

Andrii Turkovskyi
Andrii Turkovskyi

Reputation: 29438

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

Upvotes: 4

Chandrakant Dvivedi
Chandrakant Dvivedi

Reputation: 663

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();

Upvotes: -1

Related Questions