ShortDynamicLink 没有为动态 link 生成短 link

ShortDynamicLink is not generating short link for dynamic links

创建动态 link

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

之后,我得到了一个长动态link。 将此 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: Bad Request class com.google.android.gms.common.api.ApiException

你得到的长 link 是多少?我建议尝试 long link 并确保 link 正常工作。

此外,将 &d=1 附加到长 link。这样 link 将导航到可能显示 errors/warnings 的调试页面。

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

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

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

String builtLink = new LinkBuilder().setDomain("example.page.link")
           .setLink("https://www.example.com/someresource")
           .setSd(socialTagDesc)
           .setSt(socialTagTitle)
           .setSi("image link")
           .setApn("android app package")
           .setAmv("android minimum version")
           .setIbi("ios app package")
           .setImv("ios min version")
           .setIsi("ios app id").build();

DynamicLink.Builder 构建器 = FirebaseDynamicLinks.getInstance().createDynamicLink() .setLongLink(Uri.parse(builtLink));

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

public 静态 class 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);
    }
}

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