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:
_10Intent intent = new Intent(Intent.ACTION_VIEW);_10intent.setData(Uri.parse("spotify:album:0sNOF9WDwhWunNAHPD3Baj"));_10intent.putExtra(Intent.EXTRA_REFERRER,_10 Uri.parse("android-app://" + context.getPackageName()));_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 Adjust for mobile install attribution.
Open Spotify in the Google Play Store, passing your application's package name
in the adjust_campaign
parameter within the referrer
:
_18final String appPackageName = "com.spotify.music";_18final String referrer = "adjust_campaign=PACKAGE_NAME&adjust_tracker=ndjczk&utm_source=adjust_preinstall";_18_18try {_18 Uri uri = Uri.parse("market://details")_18 .buildUpon()_18 .appendQueryParameter("id", appPackageName)_18 .appendQueryParameter("referrer", referrer)_18 .build();_18 startActivity(new Intent(Intent.ACTION_VIEW, uri));_18} catch (android.content.ActivityNotFoundException ignored) {_18 Uri uri = Uri.parse("https://play.google.com/store/apps/details")_18 .buildUpon()_18 .appendQueryParameter("id", appPackageName)_18 .appendQueryParameter("referrer", referrer)_18 .build();_18 startActivity(new Intent(Intent.ACTION_VIEW, uri));_18}
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