Reputation: 2249
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("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)
{
Intent shareIntent = new Intent();
String msg = "Check this out: " + dynamicLink;
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, msg);
shareIntent.setType("text/plain");
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<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();
}
}
});
}
This second method produces an error that I don't understand.
"400: Cannot shorten a short Dynamic Link:
https://mycompany.page.link/test?afl=https%3A%2F%2Fwww.mycompany.com%2F&amv=1
&apn=com.mycompany.app&ibi=com.mycompany.app&ifl=https%3A%2F%2F
www.mycompany.com%2F&isi=963543827&ipfl=https%3A%2F%2F
www.mycompany.com%2F&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<ShortDynamicLink> 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())
.buildShortDynamicLink()
.addOnCompleteListener(Objects.requireNonNull(getActivity()), new OnCompleteListener<ShortDynamicLink>()
{
@Override
public void onComplete(@NonNull Task<ShortDynamicLink> task)
{
if (task.isSuccessful())
{
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());
}
else
{
Log.e("DynamicLink", "Link failed: " + 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
&apn=com.mycompany.app&ibi=com.mycompany.app&ifl=https%3A%2F%2F
www.mycompany.com%2F&isi=963543827&ipfl=https%3A%2F%2Fwww.mycompany.com%2F
&link=https%3A%2F%2Fwww.mycompany.com%2F
[https://firebase.google.com/docs/dynamic-links/rest#create_a_short_link_from_parameters]
Upvotes: 1
Views: 2546
Reputation: 2249
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.
Upvotes: 1
Reputation: 6071
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(...)
?
Upvotes: 0