Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 772
Simon Avatar asked Jul 09 '19 06:07

Simon


People also ask

What is difference between deep link and dynamic link?

Dynamic Links are deep links into an app that work whether or not users have installed the app yet. When users open a Dynamic Link into an app that is not installed, the app's Play Store page opens, where users can install the app. After users install and open the app, the app displays the deep-linked content.

How do I create a dynamic website link?

You create a Dynamic Link either by using the Firebase console, using a REST API, iOS or Android Builder API, or by forming a URL by adding Dynamic Link parameters to a domain specific to your app. These parameters specify the links you want to open, depending on the user's platform and whether your app is installed.

How does Firebase dynamic links work?

Dynamic Links are smart URLs that allow you to send existing and potential users to any location within your iOS or Android app. They survive the app install process, so even new users see the content they're looking for when they open the app for the first time. Dynamic Links are no-cost forever , for any scale.

What is dynamic linking in Android?

With Dynamic Links, users get the best available experience for the platform they open the link on. If the link is opened in iOS or Android browsers, they can be taken directly to the linked content in your native app.

How to create a dynamic link in Firebase?

Go to your firebase project and click on dynamic link on the right side and click on "New Dynamic Link" button. 2. Now choose default sub domain or add your own custom domain. 3.

How do I open an Android app in Firebase?

The app must be connected to your project from the Overview page of the Firebase console. Required for the Dynamic Link to open an Android app. The link to open when the app isn't installed.

How do I create a custom subdomain for my Firebase project?

Create your free subdomain in the Firebase console. All Dynamic Links features, including analytics, post-install attributions, and SDK integrations, work with both custom page.link domains and your own domain. Enable Firebase Dynamic Links for your Firebase project in the Firebase console.

What if I don't have a domain for my Firebase app?

Or, if you don't have a domain for your app, you can use a free custom page.link subdomain: Create your free subdomain in the Firebase console. All Dynamic Links features, including analytics, post-install attributions, and SDK integrations, work with both custom page.link domains and your own domain.


Video Answer


3 Answers

@Simon,

it is possible to achieve it without manual link construction, just use the same builder

private static final String OTHER_PLATFORM_LINK_KEY = "ofl";

public static Task<ShortDynamicLink> createShortDynamicLink(Uri deepLink, Uri imageUrl, String title, String description) {
    DynamicLink dynamicLink = createDynamicLink(deepLink, imageUrl, title, description);
    return FirebaseDynamicLinks.getInstance().createDynamicLink()
            .setLongLink(dynamicLink.getUri())
            .buildShortDynamicLink();
}

public static DynamicLink createDynamicLink(Uri deepLink, Uri imageUrl, String title, String description) {
    DynamicLink dynamicLink = getDynamicLinkBuilder(deepLink, imageUrl, title, description)
            .buildDynamicLink();
    String longDynamicLink = String.valueOf(dynamicLink.getUri());
    longDynamicLink += '&' + OTHER_PLATFORM_LINK_KEY + '=' + DomainConstants.OTHER_PLATFORM_LINK;
    return FirebaseDynamicLinks.getInstance().createDynamicLink()
            .setLongLink(Uri.parse(longDynamicLink))
            .buildDynamicLink();
}

public static DynamicLink.Builder getDynamicLinkBuilder(Uri deepLink, Uri imageUrl, String title, String description) {
    return FirebaseDynamicLinks.getInstance().createDynamicLink()
            .setLink(deepLink)
            .setDomainUriPrefix(DomainConstants.DYNAMIC_LINK_DOMAIN_URI_PREFIX)
            .setAndroidParameters(new DynamicLink.AndroidParameters.Builder().build())
            .setIosParameters(new DynamicLink.IosParameters.Builder(DomainConstants.IOS_BUNDLE_ID)
                    .setAppStoreId(DomainConstants.APP_STORE_ID)
                    .setMinimumVersion(DomainConstants.IOS_MINIMUM_VERSION)
                    .build())
            .setSocialMetaTagParameters(
                    new DynamicLink.SocialMetaTagParameters.Builder()
                            .setTitle(title)
                            .setDescription(description)
                            .setImageUrl(imageUrl)
                            .build())
            .setNavigationInfoParameters(new DynamicLink.NavigationInfoParameters.Builder()
                    .setForcedRedirectEnabled(true)
                    .build());
}
like image 112
Ivan Karpiuk Avatar answered Oct 17 '22 01:10

Ivan Karpiuk


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;
    }
}
like image 6
Simon Avatar answered Oct 17 '22 00:10

Simon


I solved this in a similar way but with shorter code: As other have mentioned, you must manually add an 'ofl' parameter to the link. My method was:

// Grab link from Firebase builder
guard var longDynamicLink = shareLink.url else { return }
// Parse URL to string
var urlStr = longDynamicLink.absoluteString
// Append the ofl fallback (ofl param specifies a device other than ios or android)
urlStr = urlStr + "&ofl=https://www.meetmaro.com/"
// Convert back to a URL
var urlFinal = URL(string: urlStr)!

// Shorten the url & check for errors
DynamicLinkComponents.shortenURL(urlFinal, options: nil, completion:{ [weak self] url,warnings,error in
        if let _ = error{
            return
        }
        if let warnings = warnings{
            for warning in warnings{
                print("Shorten URL warnings: ", warning)
            }
        }
        guard let shortUrl = url else {return}
        // prompt the user with UIActivityViewController
        self?.showShareSheet(url: shortUrl)
    })

The final URL can then be used to present the shareable panel with another function like: self.showShareSheet(url: finalUrl) which triggers the UIActivityViewController

Credit to http://ostack.cn/?qa=168161/ for the original idea

More about ofl: https://firebase.google.com/docs/dynamic-links/create-manually?authuser=3#general-params

like image 1
Corbin Avatar answered Oct 16 '22 23:10

Corbin