Google+ Platform for Android

Adding deep linking to Google+ posts shared from your Android app

Deep linking allows the Google+ app on mobile devices to direct clicks on a shared post that contains deep-link information to a resource within your mobile app if the user has it installed. If the user does not have your app installed, they are prompted to install it before accessing the resource. Deep linking can be used on shared basic posts and shared interactive posts.

When your users share from your app, you can enhance the links that they share so that when people follow the link or call-to-action button on any platform (Android, iOS, or desktop), they can launch your app in the appropriate context. For this cross-platform deep linking to work, you will create a client ID for each platform in the same APIs Console project and modify your apps to write deep links and to handle incoming deep links.

1. Create a client ID for each supported platform

  1. Open the API Access pane of the APIs Console

  2. In getting started, you created an OAuth 2.0 client ID for Android. To enable deep linking, click Edit settings for this client ID and click Enable in the Deep Linking section of the dialog. Click Save.

  3. If you are supporting additional platforms (iOS or Web), create a OAuth 2.0 client ID for each in this same APIs Console project. If you are also creating a native iOS app, enable deep linking for that client ID.

If you upload a logo for your app using the APIs Console, your logo might be displayed in any post to help users identify the origin and target of the post. By using the sharing feature, you agree that Google can display the name of your app and your logo within Google's products and services.

Deep links require an identifier that your app defines for use across its supported platforms to identify the correct deep-link view to launch in your app. In addition, for content deep links you must either specify a URL from which Google will retrieve the content to render the share preview, or specify data for the title, description, and thumbnail URL.

You can build content deep links in two ways:

  • Specifying a URL as the deep-link identifier. If you have a web presence that you can link to, you should use that URL for both the content URL and the deep-link identifier so that Google can retrieve the snippet data for you to use in the shared post.

  • Specifying a URI path as the deep-link identifier and passing the title, description, and thumbnail URL in a bundle. Choose this approach when you do not have a URL to retrieve the information to populate the share dialog.

The deep-link identifier must be 512 or fewer characters, and should not contain any invalid characters or character sequences for a URL. This could be any arbitrary string, or a fully qualified URL, depending on your application's needs. For example, "HelloWorld", "4815162342" or "http://example.com/mobiledata/4815162342/" are all considered valid deep-link identifiers, but not "hello world" (space) or "hello%2Xworld" (bad escape sequence). If you are using an arbitrary string for your deep-link identifier, you might consider base64-encoding it to ensure it contains only valid characters.

3. Sharing with deep linking

Configure the URL and deep-link identifier. If you do not have a URL that identifies a web representation of the resource that you are trying to link to, you can use a URI path identifier and populate the share snippet details.

Using a full URL identifier

Intent shareIntent = new PlusShare.Builder(this)
        .setText("Check out: http://example.com/cheesecake/lemon")
        .setType("text/plain")
        .setContentUrl(Uri.parse("http://example.com/cheesecake/lemon"))
        .setContentDeepLinkId(Uri.parse("http://example.com/cheesecake/lemon"))
        .getIntent();

startActivityForResult(shareIntent, 0);

The data used for the Google+ snippet is extracted from http://example.com/cheesecake/lemon.

Using a URI path identifier

Intent shareIntent = new PlusShare.Builder(this)
        .setText("Lemon Cheesecake recipe")
        .setType("text/plain")
        .setContentDeepLinkId("/cheesecake/lemon", /** Deep-link identifier */
                "Lemon Cheesecake recipe", /** Snippet title */
                "A tasty recipe for making lemon cheesecake.", /** Snippet description */
                Uri.parse("http://example.com/static/lemon_cheesecake.png"))
        .getIntent();

startActivityForResult(shareIntent, 0);

The data used for the Google+ share snippet in this example is passed directly. If the setContentURL method is specified, it overrides the snippet details.

When a user clicks on a shared post that contains deep-link data for your app, the app launches if installed, or the user is prompted to install your app. The Google+ app directs deep links to the appropriate app based on the platform that the user is browsing. For example, if they are using iOS and you have an iOS app registered in the APIs Console, the Google+ app will launch your iOS app and pass the deep-link identifier, which your app can use to direct the user to the correct location within your app. These deep links work across platforms: an interactive post generated from your Android app can launch your iOS app on an iOS user's device.

When the app launches, it needs to check if the deep-link information is available and launch the correct view in the app.

  1. To configure your app to start listening for deep links, update your AndroidManifest.xml file with the following:

    <activity android:name=".ParseDeepLinkActivity">
        <intent-filter>
            <action android:name="com.google.android.apps.plus.VIEW_DEEP_LINK" />
            <data android:scheme="vnd.google.deeplink" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>
    </activity>
    
  2. Next, add the ParseDeepLinkActivity activity to your app. This activity accepts a deep-link ID, and routes it to the correct Android Activity. In this case, because the deep link is /pages/create, we route to the CreatePageActivity:

    public class ParseDeepLinkActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            String deepLinkId = PlusShare.getDeepLinkId(this.getIntent());
            Intent target = parseDeepLinkId(deepLinkId);
            if (target != null) {
              startActivity(target);
            }
    
            finish();
        }
    
        /**
         * Get the intent for an activity corresponding to the deep-link ID.
         *
         * @param deepLinkId The deep-link ID to parse.
         * @return The intent corresponding to the deep-link ID.
         */
        private Intent parseDeepLinkId(String deepLinkId) {
            Intent route = new Intent();
            if ("/pages/create".equals(deepLinkId)) {
                route.setClass(getApplicationContext(), CreatePageActivity.class);
            } else {
                // Fallback to the MainActivity in your app.
                route.setClass(getApplicationContext(), MainActivity.class);
            }
            return route;
        }
    }
    

Next steps

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.