Reputation: 42850
Previously, I use the following dynamic link to launch a desired page in my app
http://jstock.co/a/news?code=1295.KL&symbol=PUBLIC+BANK+BHD
However, this has a shortcoming, for user who doesn't install my app. For user who doesn't install my app, this is what happen when he clicks on the link
Later, I realize Firebase dynamic link can resolve my problem. If I use the following URL, everything works fine.
https://g7b6h.app.goo.gl/?link=http%3A%2F%2Fjstock.co%2Fa%2Fnews%3Fcode%3D1295.KL%26symbol%3DPUBLIC%2BBANK%2BBHD&apn=org.yccheok.jstock.gui
For 3rd step, instead of going to the first page of the app, user can now go straight to the desired page specified in deep link.
However, I feel the above link is too long. After reading https://firebase.google.com/docs/dynamic-links/android/create, I try to shorten it using the following way
private void build() {
Task<ShortDynamicLink> shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLongLink(Uri.parse("https://g7b6h.app.goo.gl/?link=http%3A%2F%2Fjstock.co%2Fa%2Fnews%3Fcode%3D1295.KL%26symbol%3DPUBLIC%2BBANK%2BBHD&apn=org.yccheok.jstock.gui"))
.buildShortDynamicLink()
.addOnCompleteListener(this, new OnCompleteListener<ShortDynamicLink>() {
@Override
public void onComplete(@NonNull Task<ShortDynamicLink> task) {
if (task.isSuccessful()) {
Log.i("CHEOK", "success");
// Short link created
Uri shortLink = task.getResult().getShortLink();
Uri flowchartLink = task.getResult().getPreviewLink();
Log.i("CHEOK", "shortLink = " + shortLink);
Log.i("CHEOK", "flowchartLink = " + flowchartLink);
} else {
Log.i("CHEOK", "error : " + task.getException().getMessage());
// Error
// ...
}
}
});
}
However, every-time, I will get error : Bad Request
.
Any idea what step I had missed out?
Thanks.
Upvotes: 3
Views: 3554
Reputation: 970
With kotlin coroutines
private suspend fun getShortUrl(fullPath : String) : String? {
return suspendCoroutine { continuation ->
FirebaseDynamicLinks.getInstance()
.shortLinkAsync(ShortDynamicLink.Suffix.UNGUESSABLE) {
longLink = Uri.parse(fullPath)
domainUriPrefix = BuildConfig.PATH_RECEIVE_HOST
}
.addOnSuccessListener { result ->
val shortLink = result.shortLink
continuation.resume(shortLink.toString())
}.addOnFailureListener {
Log.e(TAG, "--> getShortUrl ", it)
continuation.resume(null)
}
}
Upvotes: 2
Reputation: 19980
Bad Request could well indicate an invalid API key - that is used to authorise the request. What I would do is:
/api_key/current_key
in the json fileapply
at the bottom of your app build.gradleYou can check against the steps in the documentation: https://firebase.google.com/docs/android/setup#manually_add_firebase
That should hopefully fix the API access.
While you are at it, you might want to check the debug version of your link: https://g7b6h.app.goo.gl/?link=http%3A%2F%2Fjstock.co%2Fa%2Fnews%3Fcode%3D1295.KL%26symbol%3DPUBLIC%2BBANK%2BBHD&apn=org.yccheok.jstock.gui&d=1 (&d=1 on the end). That shows a couple of warnings to resolve in the console.
Upvotes: 4
Reputation: 2828
My guess would be that the URI in your setLongLink
method does not need to be UTF-8 encoded. Firebase's example shows this:
Task<ShortDynamicLink> shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLongLink(Uri.parse("https://abc123.app.goo.gl/?link=https://example.com/&apn=com.example.android&ibn=com.example.ios"))
.buildShortDynamicLink()
Hopefully, that fixes your issue, but I never really know with Firebase.
My suggestion for dealing with this in a much simpler fashion would be to switch deep linking to Branch (full disclosure I work there, but have previously developed with Firebase). It auto generates short links because the links actually store JSON-type objects under the hood instead of within the parameters. Just a thought :)
Upvotes: 3