在Firebase动态链接中添加查询参数以链接

时间:2018-10-23 07:36:29

标签: android firebase dynamic-linking firebase-dynamic-links

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

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

如何添加我的msgid字段并获得它?

4 个答案:

答案 0 :(得分:0)

假设您要创建以下URL:

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

为此,您可以执行以下操作

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

答案 1 :(得分:0)

我找到了解决方法

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;

经过pendingDynamicLinkData.getLink()编码后,我返回了https://play.google.com/store/apps/details/?id=com.myApp&msgid=myId

答案 2 :(得分:0)

接受的答案对我而言并不理想,我要做的就是检查链接是否针对用户个人资料而不是博客文章,因此我可以重定向到我的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");
                }
            });
}

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

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

希望这会有所帮助。

答案 3 :(得分:0)

1首先将Firebase控制台中的动态链接从http://exampleandroid/test更改为http://exampleandroid/test?data     2.使用此

发送查询参数数据
 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();