Android Content Linking
This tutorial covers content linking for Android devices, where it is possible to detect Spotify.
The Spotify app registers the spotify:
URI scheme for handling
deeplinks. These URIs are found in entities returned from the Spotify Web API
under the uri field. For example, the get album endpoint returns:
_10{_10 "name": "This Is All Yours",_10 "release_date": "2014-09-22",_10 "release_date_precision": "day",_10 "type": "album",_10 "uri": "spotify:album:4oktVvRuO1In9B7Hz0xm0a"_10}
When Spotify is installed, navigating directly to content in the Spotify app provides the best user experience, since it brings Spotify to the foreground with the selected content. However, you must first determine whether Spotify is present on the device.
Detecting Spotify
Use getPackageInfo
on the PackageManager class to determine if Spotify is installed. The Spotify app for Android uses the package name com.spotify.music
.
Example:
_10PackageManager pm = getPackageManager();_10boolean isSpotifyInstalled;_10try {_10 pm.getPackageInfo("com.spotify.music", 0);_10 isSpotifyInstalled = true;_10} catch (PackageManager.NameNotFoundException e) {_10 isSpotifyInstalled = false;_10}
Opening Spotify content in the Spotify app
Once you've determined Spotify is installed, you can navigate directly to Spotify deeplinks.
Start an activity for an ACTION_VIEW
Intent, passing your app's package name in the EXTRA_REFERRER
field.
Example:
_10final String spotifyContent = "https://open.spotify.com/album/0sNOF9WDwhWunNAHPD3Baj";_10final String branchLink = "https://spotify.link/content_linking?~campaign=" + context.getPackageName() + "&$deeplink_path=" + spotifyContent + "&$fallback_url=" + spotifyContent;_10Intent intent = new Intent(Intent.ACTION_VIEW);_10intent.setData(Uri.parse(branchLink));_10startActivity(intent);
Installing Spotify
Follow the steps below if you wish to link users directly to the app store so that they can install Spotify. Spotify partners with Branch for mobile install attribution.
Open Spotify in the Google Play Store, passing your application's package name
in the ~campaign
parameter of the _branch_link
in the referrer
:
_19final String branchLink = Uri.encode("https://spotify.link/content_linking?~campaign=" + context.getPackageName());_19final String appPackageName = "com.spotify.music";_19final String referrer = "_branch_link=" + branchLink;_19_19try {_19 Uri uri = Uri.parse("market://details")_19 .buildUpon()_19 .appendQueryParameter("id", appPackageName)_19 .appendQueryParameter("referrer", referrer)_19 .build();_19 startActivity(new Intent(Intent.ACTION_VIEW, uri));_19} catch (android.content.ActivityNotFoundException ignored) {_19 Uri uri = Uri.parse("https://play.google.com/store/apps/details")_19 .buildUpon()_19 .appendQueryParameter("id", appPackageName)_19 .appendQueryParameter("referrer", referrer)_19 .build();_19 startActivity(new Intent(Intent.ACTION_VIEW, uri));_19}
Fallback experience: Web Links
If the user doesn’t have Spotify installed or you’re otherwise unable to detect Spotify, you should open Spotify web links (open.spotify.com) in the system browser or a webview.
Web links are provided in Web API entities under the external_urls.spotify
field.
_10{_10 "name": "She's So Unusual",_10 "type": "album",_10 ..._10 "external_urls" : {_10 "spotify" : "https://open.spotify.com/album/0sNOF9WDwhWunNAHPD3Baj"_10 }_10}
Attribution
In order for Spotify to attribute traffic to your app, pass your application's
package name in the utm_campaign
query string parameter. For example:
https://open.spotify.com/track/55fmthmn3rgnk9Wyx7G5dU?utm_campaign=com.app
Allow deeplinking from Webviews
Content at open.spotify.com
links directly into Spotify or the app store. By
default, Android restricts webviews and prevent deeplinking into apps.
Support deeplinks by ensuring the webview handles the following schemes:
spotify:
market:
Further read
Android: https://stackoverflow.com/a/20134598