androiddeep-linkingfirebase-dynamic-links

Shorten the Firebase dynamic link


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

  1. Go to Google Play store
  2. Install the app.
  3. Launch the app. Will go to the first page of the app, instead of the desired page specified in deep 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.


Solution

  • Bad Request could well indicate an invalid API key - that is used to authorise the request. What I would do is:

    1. Re-download google-services.json from the Firebase Console and overwrite the one in your project
    2. Check that the API key parameter is filled for the entry for your package name under /api_key/current_key in the json file
    3. Check that you have included the google-services plugin and called apply at the bottom of your app build.gradle

    You 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.