如何使用Firebase从动态链接中获取额外参数?

时间:2016-07-07 16:53:10

标签: android firebase firebase-dynamic-links

我手动创建了动态链接,并在链接上设置了一些其他参数,如下所示:https://XXXXX.goo.gl/?link=https%3A%2F%2Fairbanq.send.com%2Fsendmoney&apn=com.xxxx.xxxx&amv=1&username=Adri&amount=7.00

但是,当应用程序打开时,我得到:“https:// airbanq.send.com/sendmoney” 没有附加参数

我正在使用此示例代码 https://github.com/firebase/quickstart-android/tree/master/dynamiclinks

任何帮助,请

由于

我的代码

public String buildDeepLink() {
    // Get the unique appcode for this app.
    String appCode = AirBanqApp.mContext.getString(R.string.app_code);

    // Get this app's package name.
    String packageName = AirBanqApp.mContext.getPackageName();

    // Build the link with all required parameters
    Uri.Builder builder = new Uri.Builder()
            .scheme("https")
            .authority(appCode + ".app.goo.gl")
            .path("/")
            .appendQueryParameter("link", deepLink)
            .appendQueryParameter("apn", packageName);

    // If the deep link is used in an advertisement, this value must be set to 1.
    if (isAd) {
        builder.appendQueryParameter("ad", "1");
    }

    // Minimum version is optional.
    if (minVersion > 0) {
        builder.appendQueryParameter("amv", Integer.toString(minVersion));
    }

    if (!TextUtils.isEmpty(androidLink)) {
        builder.appendQueryParameter("al", androidLink);
    }

    if (!TextUtils.isEmpty(playStoreAppLink)) {
        builder.appendQueryParameter("afl", playStoreAppLink);
    }

    if (!customParameters.isEmpty()) {
        for (Map.Entry<String, String> parameter : customParameters.entrySet()) {
            builder.appendQueryParameter(parameter.getKey(), parameter.getValue());
        }
    }

    // Return the completed deep link.
    return builder.build().toString();
}

2 个答案:

答案 0 :(得分:17)

多数民众赞成是我的解决方案

我解决了我的问题,我假设了#34; apn&#34;,&#34;用户名&#34;和&#34;金额&#34;它们是参数&#34; LINK&#34;的一部分。在网址中,但是当我添加&#34;&amp;&#34;我正在向主网址添加部分,以便为&#34; LINK&#34;添加参数。字段我需要首先创建像这样的网址

https://airbanq.send.com/sendmoney?username=Adri&amount=7.00

然后使用URLEncoder.encode(queryParameters.toString(),&#34; UTF-8&#34;); 生成这个 HTTPS%3A%2F%2Fairbanq.send.com%2Fsendmoney%253Fusername%253DAdri%2526amount%253D7.00

然后附加到主网址

https://xxxx.app.goo.gl/?link=https%3A%2F%2Fairbanq.send.com%2Fsendmoney%253Fusername%253DAdri%2526amount%253D7.00&apn=com.airbanq.airbanqapp&amv=1

 public String buildDeepLink() {
    // Get the unique appcode for this app.
    String appCode = AirBanqApp.mContext.getString(R.string.app_code);

    // Get this app's package name.
    String packageName = AirBanqApp.mContext.getPackageName();
    String queryParamters = "";
    try {
        queryParamters = generateQueryParameters();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    if (!TextUtils.isEmpty(queryParamters)) {
        deepLink = deepLink + queryParamters;
    }
    // Build the link with all required parameters
    Uri.Builder builder = new Uri.Builder()
            .scheme("https")
            .authority(appCode + ".app.goo.gl")
            .path("/")
            .appendQueryParameter("link", deepLink)
            .appendQueryParameter("apn", packageName);

    // If the deep link is used in an advertisement, this value must be set to 1.
    if (isAd) {
        builder.appendQueryParameter("ad", "1");
    }

    // Minimum version is optional.
    if (minVersion > 0) {
        builder.appendQueryParameter("amv", Integer.toString(minVersion));
    }

    if (!TextUtils.isEmpty(androidLink)) {
        builder.appendQueryParameter("al", androidLink);
    }

    if (!TextUtils.isEmpty(playStoreAppLink)) {
        builder.appendQueryParameter("afl", playStoreAppLink);
    }

    // Return the completed deep link.
    return builder.build().toString();
}

private String generateQueryParameters() throws UnsupportedEncodingException {
    StringBuilder queryParameters = new StringBuilder();
    //server purposes
    queryParameters.append("?*code*");

    if (!customParameters.isEmpty()) {
        for (Map.Entry<String, String> parameter : customParameters.entrySet()) {
            queryParameters.append(String.format("&%1s=%2s", parameter.getKey(), parameter.getValue()));
        }
    }
    return URLEncoder.encode(queryParameters.toString(), "UTF-8");
}

答案 1 :(得分:0)

官方的答案是,您需要对URL字符串进行转义/编码,以便可以将其安全地放置在URL查询中。 我希望Firebase动态链接可以只说一下链接。

对于Golang: url.QueryEscape(urlstring)