Can the links for Firebase deep links be shortened? Do they have that feature? Links generated are too long and that is not good.
5 Answers
UPDATE
Firebase now supports shorten dynamic links programmatically.
I've faced the same problem getting a long and not user friendly URL when creating a dynamic link programmatically.
The solution I've found is to use the Google URL Shortener API that works brilliant. That link points to the Java library, I'm using it in Android, but you can do also a simple http request.
I'll post my Android code in case you need it:
private void createDynamicLink() {
// 1. Create the dynamic link as usual
String packageName = getApplicationContext().getPackageName();
String deepLink = "YOUR DEEPLINK";
Uri.Builder builder = new Uri.Builder()
.scheme("https")
.authority(YOUR_DL_DOMAIN)
.path("/")
.appendQueryParameter("link", deepLink)
.appendQueryParameter("apn", packageName);
final Uri uri = builder.build();
//2. Create a shorten URL from the dynamic link created.
Urlshortener.Builder builderShortener = new Urlshortener.Builder (AndroidHttp.newCompatibleTransport(), AndroidJsonFactory.getDefaultInstance(), null);
final Urlshortener urlshortener = builderShortener.build();
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
Url url = new Url();
url.setLongUrl(uri.toString());
try {
Urlshortener.Url.Insert insert=urlshortener.url().insert(url);
insert.setKey("YOUR_API_KEY");
url = insert.execute();
Log.e("url.getId()", url.getId());
return url.getId();
} catch (IOException e) {
e.printStackTrace();
return uri.toString();
}
}
@Override
protected void onPostExecute(String dynamicLink) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getResources().getString(R.string.share_subject));
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, dynamicLink);
startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using)));
Log.e("dynamicLink", dynamicLink);
}
}.execute(null, null, null);
}
Hope it helps!!

- 2,669
- 3
- 17
- 24
-
URL Shortener is deprecated. Refer https://developers.google.com/api-client-library/java/apis/firebasedynamiclinks/v1 this link for firebase dynamic link – siddiq Apr 03 '19 at 04:47
-
is it a must to shorten a deeplink? my link already a very small one? – Suryakant Sharma Mar 31 '20 at 14:05
The links can be shortened in the Firebase console in the Dynamic Links tab. Tap on 'New Dynamic Link', which gives you an option to create a short link from an existing link.

- 403
- 3
- 7
-
1But can we do that programmatically? if i want to share my link on Facebook, it will have long links – shibapoo Jun 01 '16 at 08:13
-
So why can't you generate the short link on Firebase console and share that link on facebook? – diidu Jun 02 '16 at 11:22
-
@user14202a Yes you are right - this cannot be done programmatically today. Filed a feature request. Watch out for more updates on this. – Arun Venkatesan Jun 03 '16 at 18:32
This can be done programmatically using the Firebase Dynamic Links REST API, for example:
POST https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=api_key
Content-Type: application/json
{
"longDynamicLink": "https://abc123.app.goo.gl/?link=https://example.com/&apn=com.example.android&ibi=com.example.ios"
}
See https://firebase.google.com/docs/dynamic-links/short-links
And I just had to code it for Android - here's the code in case it helps someone:
at the top of the activity:
lateinit private var dynamicLinkApi: FbDynamicLinkApi
private var remoteCallSub: Subscription? = null // in case activity is destroyed after remote call made
in onCreate (not really but to keep it simple, actually you should inject it):
val BASE_URL = "https://firebasedynamiclinks.googleapis.com/"
val retrofit = Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()))
.build()
dynamicLinkApi = retrofit.create(FbDynamicLinkApi::class.java)
then when it's time to shorten a URL:
remoteCallSub = dynamicLinkApi.shortenUrl(getString(R.string.fbWebApiKey), UrlRo(dynamicLink))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ url -> Log.d("Shortened: $url") },
{ e -> Log.e("APP","Error with dynamic link REST api", e) })
Don't forget to unsubscribe in onDestroy:
override fun onDestroy() {
super.onDestroy()
remoteCallSub?.unsubscribe()
}
And here's the classes you need for the dynamic API:
interface FbDynamicLinkApi {
@POST("v1/shortLinks")
fun shortenUrl(@Query("key") key: String, @Body urlRo: UrlRo): Observable<ShortUrlRo>
}
data class UrlRo(val longDynamicLink: String, val suffix: SuffixRo = SuffixRo())
data class SuffixRo(val option: String = "UNGUESSABLE")
data class ShortUrlRo(val shortLink: String, val warnings: List<WarningRo>, val previewLink: String)
data class WarningRo(val warningCode: String, val warningMessage: String)

- 350
- 1
- 8
Try This , its working fine in my case, https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=[api-key]
{
"dynamicLinkInfo": {
"dynamicLinkDomain": "peg3z.app.goo.gl",
"link": "[Your Long Url Which you want to make short]",
"androidInfo": {
"androidPackageName": "com.xyz"//
},
"iosInfo": {
"iosBundleId": "com.dci.xyz"
}
}
}
'Content-Type: text/plain'

- 11
- 4