App Links on Android

Your app can post stories to 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

First you need to prepare your development environment by linking to or downloading the Facebook App Links SDK for Android.

The App Links SDK for Android is a component of the Facebook SDK for Android. To use the Facebook Places SDK in your project, make it a dependency in Maven, or download it. Choose the method you prefer with the following button.

Link the SDK with Maven

  1. In your project, open your_app | Gradle Scripts | build.gradle (Project) and add the following repository to the buildscript { repositories {}} section to download the SDK from the Maven Central Repository:
    mavenCentral() 
  2. In your project, open your_app | Gradle Scripts | build.gradle (Module: app) and add the following compile statement to the dependencies{} section to compile the latest version of the SDK:
    compile 'com.facebook.android:facebook-applinks:[4,5)'
  3. Build your project.

Download the SDK

To download the SDK, click the following button.

Download SDK

When you use the Facebook App Links SDK, events in your app are automatically logged and collected for Facebook Analytics unless you disable automatic event logging. For details about what information is collected and how to disable automatic event logging, see Automatic App Event Logging.

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.

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());
    }
}