App Links on Android

Your app can post stories to news feed. When people click on those stories, Facebook can send people to either your app or your app's App Store page. This drives traffic and app installs. You can implement this behavior using App Links.

When you set up App Links, you can control what happens when someone taps on one of the links shared through your app or on the story attribution (name of your app) in one of the Open Graph stories shared through your app. The person may be redirected to the web version of your content or they may be directed to your app. If the person doesn't have your app installed, they will be redirected to your app's Google Play listing. The image below shows this flow.

In either case, once the person reaches your app (directly or after the app install), information will be passed in the Intent that can be used by your app to decide what to show the person to provide continuity in the user experience. For example, if I see a story on my Facebook feed about one of my friends completing this share tutorial and I tap on it, I will expect to be redirected to a view in your app that features this tutorial and not to your app's main activity.

In the following sections we will explain how to handle incoming links once you've set up your App Links.

Handling incoming links

When someone taps a link posted from your app or the app attribution in an Open Graph story posted from your app in Facebook for Android, they'll either be directed to your app or a web version of your content. The redirect behavior depends on how you've configured your App Links and on whether or not you have a mobile-only setup. If your app is mobile only, set should_fallback=false so an app redirect takes the person to your app's Google Play listing if it isn't installed. When your app is launched, intent information corresponding to the App Link is sent to your app.

To ensure an engaging user experience, you should process the incoming intent information when your app is activated and direct the person to the object featured in the story they're coming from. For example, if I see a story on my Facebook feed about one of my friends completing this share tutorial and I tap on it, I will expect to be redirected to a view in your app that features this tutorial and not to your app's main activity.

The information in the Intent will look like this:

data: "sharesample://story/1234?target_url=https%3A%2F%2Fdevelopers.facebook.com%2Fandroid"
extras:
  al_applink_data: <Bundle containing App Link data>

The intent's data field contains the Uri for your App Link's Android URL as well as the target URL. If you marked up your metadata on a web page and didn't specify an Android URL, this field will simply be the target URL, or https://developers.facebook.com/android in our example.

If you use the App Links Hosting API, the Intent data will look like this:

data: "sharesample://story/1234?target_url=https%3A%2F%2Ffb.me%2F643402985734299"
extras:
  al_applink_data: <Bundle containing App Link data>

The difference here is that the target URL is the canonical URL provided by the App Links Hosting API.

The intent's extras contains a Bundle with information that looks like this:

target_url: "https://developers.facebook.com/android"
extras:
  fb_app_id: [YOUR_FACEBOOK_APP_ID]
  fb_access_token: "[ACCESS_TOKEN]"
  fb_expires_in: 3600

It includes the target URL, and another Bundle that includes your Facebook app ID and an access_token if the person had authenticated with your app.

You can modify your activity's onCreate method to handle the incoming link.

The Bolts library, which is packaged in Facebook SDK, provides easy to use APIs that can help you to parse the incoming URL.

In the code sample below, we're simply logging the target URL, but you should direct people through the appropriated flow for your app:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(this); 
    ...
    Uri targetUrl =
      AppLinks.getTargetUrlFromInboundIntent(this, getIntent());
    if (targetUrl != null) {
        Log.i("Activity", "App Link Target URL: " + targetUrl.toString());
    }
}