将查询参数添加到 firebase 动态链接中的链接

And*_*sky 5 android dynamic-linking firebase firebase-dynamic-links

我创建了动态链接,我想发送一些特定的参数,例如:“ https://mydynamiclink/?link= ” + 链接 + “&msgid=” + id + “&apn=myapn”。 link字段看起来像“ https://play.google.com/store/apps/details/?id=com.myApp&msgid=myId&apn=myapn

当我点击此链接后打开我的应用程序时 - 我收到PendingDynamicLinkData并可以从中获取链接,但不能获取一些自定义数据。(pendingDynamicLinkData.getLink()返回没有“&msgid=...”的链接 - 我得到字符串“ https://play.google.com/store/apps/details/?id=com.myApp ”)

我怎样才能添加我的 msgid 字段并获取它呢?

Pet*_*gba 5

接受的答案对我来说效果不佳,我所需要做的就是检查链接是否是用户的个人资料而不是博客文章,这样我就可以重定向到我的 ProfileActivity。

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");
                }
            });
}
Run Code Online (Sandbox Code Playgroud)

当用户使用生成的链接导航到我的应用程序时,它会转到帖子详细信息活动,因为我将该活动设置为清单中唯一可浏览的活动。然后,我必须使用路由查询来确定传入链接是博客文章还是共享用户个人资料。

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()
            );
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。


And*_*sky 4

我找到了解决方案

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;
Run Code Online (Sandbox Code Playgroud)

在这样的编码pendingDynamicLinkData.getLink()返回我之后https://play.google.com/store/apps/details/?id=com.myApp&msgid=myId