Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 858
Andrey Turkovsky Avatar asked Oct 23 '18 07:10

Andrey Turkovsky


2 Answers

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

like image 162
Andrey Turkovsky Avatar answered Sep 29 '22 07:09

Andrey Turkovsky


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.

like image 34
Peterstev Uremgba Avatar answered Sep 29 '22 08:09

Peterstev Uremgba