ShortDynamicLink不会为动态链接生成短链接

时间:2017-08-20 15:39:23

标签: android firebase firebase-dynamic-links

创建动态链接

  DynamicLink dynamicLink = FirebaseDynamicLinks.getInstance().createDynamicLink()
            .setLink(Uri.parse("http://shaharapp.in/"))
            .setDynamicLinkDomain("https://cuh8g.app.goo.gl/")
            // Open links with this app on Android
            .setAndroidParameters(new DynamicLink.AndroidParameters.Builder().build())
            // Open links with com.example.ios on iOS
            .buildDynamicLink();

     dynamicLinkUri = dynamicLink.getUri();

在此之后,我获得了一个很长的动态链接。 将此dynamicLinkUri传递给shortLinkTask

 Task<ShortDynamicLink> shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
            .setLongLink(dynamicLinkUri)
            .buildShortDynamicLink();
    shortLinkTask.addOnCompleteListener(new OnCompleteListener<ShortDynamicLink>() {
        @Override
        public void onComplete(@NonNull Task<ShortDynamicLink> task) {
            if (task.isSuccessful()) {


                Uri shortLink = task.getResult().getShortLink();
                link_txt.setText(shortLink.toString());
            }
            else {

                ViewGroup container = (ViewGroup) findViewById(R.id.snackbar_layout);
                Snackbar.make(container, "error", Snackbar.LENGTH_SHORT).show();

            }
        }
    });

此任务未完成,经过调试后我得到了这个

  

com.google.android.gms.common.api.ApiException:7:错误请求   com.google.android.gms.common.api.ApiException class

3 个答案:

答案 0 :(得分:4)

您获得的长链接是什么?我建议尝试长链接并确保链接正常工作。

另外,将&d=1附加到长链接。此类链接将导航到调试页面,并显示可能的错误/警告。

查看您的代码,失败的一个可能原因是域的格式。请参见示例https://firebase.google.com/docs/dynamic-links/android/create。域期望是仅包含域的字符串,没有https方案:

.setDynamicLinkDomain("abc123.app.goo.gl")

答案 1 :(得分:0)

确保您的.setDynamicLinkDomain(“ https://abc123.app.goo.gl”)包含“ https://”

答案 2 :(得分:-1)

我不想降级Firebase依赖项时的解决方案

String builtLink =新的LinkBuilder()。setDomain(“ example.page.link”)                .setLink(“ https://www.example.com/someresource”)                .setSd(socialTagDesc)                .setSt(socialTagTitle)                .setSi(“图像链接”)                .setApn(“ android应用程序包”)                .setAmv(“ android最低版本”)                .setIbi(“ ios应用程序包”)                .setImv(“ iOS最低版本”)                .setIsi(“ ios app id”)。build();  DynamicLink.Builder构建器= FirebaseDynamicLinks.getInstance()。createDynamicLink()                 .setLongLink(Uri.parse(builtLink));

    builder.buildShortDynamicLink()
            .addOnSuccessListener(listener)
            .addOnFailureListener(failureListener);

公共静态类LinkBuilder {

    private String domain;
    private String link;
    private String apn;
    private String amv;

    private String ibi;
    private String imv;
    private String isi;


    private String st;
    private String sd;
    private String si;

    public String getURLEncode(String input){

        try
        {
            return URLEncoder.encode(input, "UTF-8");
        }
        catch (Exception ex){
            Timber.e(ex);
        }

        return input;
    }


    public LinkBuilder setDomain(String domain) {
        this.domain = domain;
        return this;
    }

    public LinkBuilder setLink(String link) {
        this.link = getURLEncode(link);
        return this;
    }

    public LinkBuilder setApn(String apn) {
        this.apn = apn;
        return this;
    }

    public LinkBuilder setAmv(String amv) {
        this.amv = amv;
        return this;
    }

    public LinkBuilder setIbi(String ibi) {
        this.ibi = ibi;
        return this;
    }

    public LinkBuilder setImv(String imv) {
        this.imv = imv;
        return this;
    }

    public LinkBuilder setIsi(String isi) {
        this.isi = isi;
        return this;
    }

    public LinkBuilder setSt(String st) {
        this.st = getURLEncode(st);
        return this;
    }

    public LinkBuilder setSd(String sd) {
        this.sd = getURLEncode(sd);;
        return this;
    }

    public LinkBuilder setSi(String si) {
        this.si = getURLEncode(si);;
        return this;
    }

    public String build(){
        return String.format("https://%s/?link=%s&apn=%s&amv=%s&ibi=%s&imv=%s&isi=%s&st=%s&sd=%s&si=%s"
                ,domain, link, apn, amv, ibi, imv,isi,st,sd,si);
    }
}