Firebase Deep Link short URL

shibapoo picture shibapoo · May 31, 2016 · Viewed 14k times · Source

Can the links for Firebase deep links be shortened? Do they have that feature? Links generated are too long and that is not good.

Answer

MrBrightside picture MrBrightside · Aug 16, 2016

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!!