如何创建一个Firebase短链接动态链接

huangapple go评论135阅读模式
英文:

How to create a Firebase short dynamic link

问题

在Android中,我成功地使用Java创建了一个Firebase动态链接。以下是我用于实现此操作的代码:

shareButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        DynamicLink dynamicLink = FirebaseDynamicLinks.getInstance().createDynamicLink()
            .setLink(Uri.parse("https://www.mycompany.com/"))
            .setDomainUriPrefix("https://mycompany.page.link/test")
            .setAndroidParameters(
                new DynamicLink.AndroidParameters.Builder("com.mycompany.app")
                    .setFallbackUrl(Uri.parse("https://www.mycompany.com/"))
                    .setMinimumVersion(1)
                    .build())
            .buildDynamicLink();

        Uri dynamicLinkUri = dynamicLink.getUri();
        shareDynamicLink(dynamicLinkUri);
    }
});

public void shareDynamicLink(Uri dynamicLink) {
    Task<ShortDynamicLink> shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
        .setLongLink(dynamicLink)
        .buildShortDynamicLink()
        .addOnCompleteListener(Objects.requireNonNull(this.getActivity()), new OnCompleteListener<ShortDynamicLink>() {
            @Override
            public void onComplete(@NonNull Task<ShortDynamicLink> task) {
                if (task.isSuccessful()) {
                    // Short link created
                    Uri shortLink = Objects.requireNonNull(task.getResult()).getShortLink();
                    Uri flowchartLink = task.getResult().getPreviewLink();

                    Log.e("DynamicLink", "shortLink: " + shortLink + System.lineSeparator());
                    Log.e("DynamicLink", "flowChartLink: " + flowchartLink + System.lineSeparator());

                    Intent shareIntent = new Intent();
                    String msg = "Check this out: " + shortLink;
                    shareIntent.setAction(Intent.ACTION_SEND);
                    shareIntent.putExtra(Intent.EXTRA_TEXT, msg);
                    shareIntent.setType("text/plain");
                    startActivity(shareIntent);
                } else {
                    Toast.makeText(context, "Failed to share event.", Toast.LENGTH_SHORT).show();
                }
            }
        });
}

但是,在尝试缩短动态链接时,您遇到了一个错误:

"400: Cannot shorten a short Dynamic Link:
https://mycompany.page.link/test?afl=https%3A%2F%2Fwww.mycompany.com%2F&amv=1
&amp;apn=com.mycompany.app&amp;ibi=com.mycompany.app&amp;ifl=https%3A%2F%2F
www.mycompany.com%2F&amp;isi=963543827&amp;ipfl=https%3A%2F%2F
www.mycompany.com%2F&amp;link=https%3A%2F%2Fwww.mycompany.com%2F
[https://firebase.google.com/docs/dynamic-links/rest#create_a_short_link_from_parameters]"

这个错误是因为您尝试缩短一个已经是短链接的动态链接。Firebase动态链接缩短服务不能再次缩短已经是短链接的链接。在您的第一个 shareDynamicLink 方法中,您已经获得了长链接并尝试缩短它,这是正确的。但是,当您尝试在第二次调用 shareDynamicLink 方法中缩短已经是短链接的链接时,会导致错误。

确保只在需要时缩短长链接,不要尝试缩短已经是短链接的链接。这应该解决问题。

英文:

I'm successfully creating a Firebase dynamic link in Java on Android. My code to do so is in a button click listener.

shareButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
DynamicLink dynamicLink = FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLink(Uri.parse(&quot;https://www.mycompany.com/&quot;))
.setDomainUriPrefix(&quot;https://mycompany.page.link/test&quot;)
.setAndroidParameters(
new DynamicLink.AndroidParameters.Builder(&quot;com.mycompany.app&quot;)
.setFallbackUrl(Uri.parse(&quot;https://www.mycompany.com/&quot;))
.setMinimumVersion(1)
.build())
.buildDynamicLink();
Uri dynamicLinkUri = dynamicLink.getUri();
shareDynamicLink(dynamicLinkUri);
}
});
public void shareDynamicLink(Uri dynamicLink)
{
Intent shareIntent = new Intent();
String msg = &quot;Check this out: &quot; + dynamicLink;
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, msg);
shareIntent.setType(&quot;text/plain&quot;);
startActivity(shareIntent);
}

This sends a LONG dynamic link that works just fine. Now I'd like to shorten the link, so I replaced the 'shareDynamicLink' method with this code.

public void shareDynamicLink(Uri dynamicLink)
{
Task&lt;ShortDynamicLink&gt; shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLongLink(dynamicLink)
.buildShortDynamicLink()
.addOnCompleteListener(Objects.requireNonNull(this.getActivity()), new OnCompleteListener&lt;ShortDynamicLink&gt;()
{
@Override
public void onComplete(@NonNull Task&lt;ShortDynamicLink&gt; task)
{
if (task.isSuccessful())
{
// Short link created
Uri shortLink = Objects.requireNonNull(task.getResult()).getShortLink();
Uri flowchartLink = task.getResult().getPreviewLink();
Log.e(&quot;DynamicLink&quot;, &quot;shortLink: &quot; + shortLink + System.lineSeparator());
Log.e(&quot;DynamicLink&quot;, &quot;flowChartLink: &quot; + flowchartLink + System.lineSeparator());
Intent shareIntent = new Intent();
String msg = &quot;Check this out: &quot; + shortLink;
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, msg);
shareIntent.setType(&quot;text/plain&quot;);
startActivity(shareIntent);
}
else
{
Toast.makeText(context, &quot;Failed to share event.&quot;, Toast.LENGTH_SHORT).show();
}
}
});
}

This second method produces an error that I don't understand.

&quot;400: Cannot shorten a short Dynamic Link:
https://mycompany.page.link/test?afl=https%3A%2F%2Fwww.mycompany.com%2F&amv=1
&amp;apn=com.mycompany.app&amp;ibi=com.mycompany.app&amp;ifl=https%3A%2F%2F
www.mycompany.com%2F&amp;isi=963543827&amp;ipfl=https%3A%2F%2F
www.mycompany.com%2F&amp;link=https%3A%2F%2Fwww.mycompany.com%2F
[https://firebase.google.com/docs/dynamic-links/rest#create_a_short_link_from_parameters]

What am I missing here? This seems like it should work.

Note: I don't need the long dynamic link, just the short one. I tried changing the onClickListener as follows.

shareButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Task&lt;ShortDynamicLink&gt; dynamicLink = FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLink(Uri.parse(&quot;https://www.mycompany.com/&quot;))
.setDomainUriPrefix(&quot;https://mycompany.page.link/test&quot;)
.setAndroidParameters(
new DynamicLink.AndroidParameters.Builder(&quot;com.mycompany.app&quot;)
.setFallbackUrl(Uri.parse(&quot;https://www.mycompany.com/&quot;))
.setMinimumVersion(1)
.build())
.buildShortDynamicLink()
.addOnCompleteListener(Objects.requireNonNull(getActivity()), new OnCompleteListener&lt;ShortDynamicLink&gt;()
{
@Override
public void onComplete(@NonNull Task&lt;ShortDynamicLink&gt; task)
{
if (task.isSuccessful())
{
Uri shortLink = Objects.requireNonNull(task.getResult()).getShortLink();
Uri flowchartLink = task.getResult().getPreviewLink();
Log.e(&quot;DynamicLink&quot;, &quot;shortLink: &quot; + shortLink + System.lineSeparator());
Log.e(&quot;DynamicLink&quot;, &quot;flowChartLink: &quot; + flowchartLink + System.lineSeparator());
}
else
{
Log.e(&quot;DynamicLink&quot;, &quot;Link failed: &quot; + task.getException().getMessage() + System.lineSeparator());
}
}
});
}
});

But I still get the same 400 error.

400: Cannot shorten a short Dynamic Link:
https://mycompany.page.link/test?afl=https%3A%2F%2Fwww.mycompany.com%2F&amv=1
&amp;apn=com.mycompany.app&amp;ibi=com.mycompany.app&amp;ifl=https%3A%2F%2F
www.mycompany.com%2F&amp;isi=963543827&amp;ipfl=https%3A%2F%2Fwww.mycompany.com%2F
&amp;link=https%3A%2F%2Fwww.mycompany.com%2F
[https://firebase.google.com/docs/dynamic-links/rest#create_a_short_link_from_parameters]

答案1

得分: 1

问题出在我对程序生成的链接与预定义链接的工作方式有所误解。在我的情况下,我试图使用来自Firebase控制台的预定义链接("https://mycompany.page.link/test")作为我生成链接的前缀。当我尝试缩短它时,这在后端引发了某种混淆。我仍然不完全明白它不喜欢什么,但要点是它失败了。

因此,生成链接的解决方案是仅使用来自Firebase控制台的基本前缀 - .setDomainPrefix("https://mycompany.page.link/")。使用这个前缀,我可以创建".buildShortDynamicLink()"或"buildDynamicLink()"。

我在Firebase控制台中创建的链接("http://mycompany.page.link/test")只能逐字使用 - 无需生成任何内容。只需将它字面放入文本消息中,你就完成了。

英文:

For anyone who finds this, my problem stemmed from a misunderstanding of how programmatically generated links work vs. predefined links. In my case I was trying to use a pre-defined link from the Firebase console ("https://mycompany.page.link/test") as the PREFIX for my generated link. That caused some sort of confusion on the back end when I tried to shorten it. I still don't understand exactly what it didn't like, but point is it failed.

So the solution for generating links was to use only the base prefix from the Firebase console - .setDomainPrefix("https://mycompany.page.link/"). Using that I can create either ".buildShortDynamicLink()" or "buildDynamicLink()".

The link I created in the Firebase console ("http://mycompany.page.link/test") can only be used verbatim -- no need to generate anything. Just put it in a text message literally and you're done.

答案2

得分: 0

你打算完全使用较长的版本,还是只使用短版本?如果是这样,是否在onClick(...)方法中使用.buildShortDynamicLink()而不是.buildDynamicLink(),然后在shareDynamicLink(...)中进行转换会起作用吗?

英文:

Are you going to use the longer version at all, or only the short one? If so, does it work if you use .buildShortDynamicLink() instead of .buildDynamicLink() in your onClick(...) method without the conversion in shareDynamicLink(...)?

huangapple
  • 本文由 发表于 2020年7月30日 04:51:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63162243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定