Search code examples
firebase-dynamic-linksandroid-instant-apps

How to make my firebase dynamic link redirect to my website on desktop and to my instant app on mobile


I have an instant app and a Firebase dynamic link which redirects to this instant app.

But when I click the dynamic link on a computer, the link leads to a non existant page of my website.

According to Google doc : https://firebase.google.com/docs/dynamic-links/android/create

When users open a Dynamic Link on a desktop web browser, they will load this URL (unless the ofl parameter is specified). If you don't have a web equivalent to the linked content, the URL doesn't need to point to a valid web resource. In this situation, you should set up a redirect from this URL to, for example, your home page.

So I created a redirection for my dynamic link which redirects /share/** to /

And it works, when I click the link on a computer I land on the homepage of my website. But my Dynamic links also leads on my homepage and do not open my instant app anymore.

So my question is : how to configure a redirection which redirects desktop users from /share/** to / without breaking my instant app ?


Solution

  • Add the "ofl" parameter to the url to make the FDL redirects another URL on desktop.

    Unfortunately, it is not possible to add this parameter with the Android builder.

    So you have to manually create the link, and use the "setLongLink" method as you can see here at the bottom of the page : https://firebase.google.com/docs/dynamic-links/android/create#shorten-a-long-dynamic-link

    Task<ShortDynamicLink> shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
            .setLongLink(Uri.parse("https://example.page.link/?link=https://www.example.com/&apn=com.example.android&ibn=com.example.ios"))
            .buildShortDynamicLink()
            .addOnCompleteListener(this, new OnCompleteListener<ShortDynamicLink>() {
                @Override
                public void onComplete(@NonNull Task<ShortDynamicLink> task) {
                    if (task.isSuccessful()) {
                        // Short link created
                        Uri shortLink = task.getResult().getShortLink();
                        Uri flowchartLink = task.getResult().getPreviewLink();
                    } else {
                        // Error
                        // ...
                    }
                }
            });
    

    I created my own builder to include the ofl parameter. If it can helps :

    public class DynamicLinkBuilder {
    
        private String dynamicLink = "https://example.com/foo"
    
        public DynamicLinkBuilder(String link) {
            this.dynamicLink += "?link=" + link;
        }
    
        public DynamicLinkBuilder addMinVersion(int minVersion){
            dynamicLink += "&amv=" + minVersion;
            return this;
        }
    
        public DynamicLinkBuilder addIosUrl(String iosUrl){
            dynamicLink += "&ifl=" + iosUrl;
            return this;
        }
    
        public DynamicLinkBuilder addDesktopUrl(String desktopUrl){
            dynamicLink += "&ofl=" + desktopUrl;
            return this;
        }
    
        public DynamicLinkBuilder addFallbackUrl(String fallbackUrl){
            dynamicLink += "&afl=" + fallbackUrl;
            return this;
        }
    
        public DynamicLinkBuilder addPackageName(String packageName){
            dynamicLink += "&apn=" + packageName;
            return this;
        }
    
        public DynamicLinkBuilder addSocialMediaLogo(String logoUrl){
            dynamicLink += "&si=" + logoUrl;
            return this;
        }
    
        public DynamicLinkBuilder addSocialMediaTitle(String title){
            dynamicLink += "&st=" + Uri.encode(title);
            return this;
        }
    
        public DynamicLinkBuilder addSocialMediaDescription(String description){
            dynamicLink += "&sd=" + Uri.encode(description);
            return this;
        }
    
        public String build(){
            return dynamicLink;
        }
    }