Xamarin.Android App Links

 

Android App Links allows the ability for a website URL to either open a webpage or open directly into your app if the user has it installed. It is very similar iOS’s Universal App Links. Lets run through how to use these within your Xamarin Android app.

Note: You may come across material mentioning a component called Rivets when searching for Xamarin Android App Links and this will include documentation on the Xamarin website. However this component has been superseded by other components as you will discover.

Create An Intent

First we need to tell our app that we want to register a certain URL when we install the app. We do this by adding an intent to the activity we want to launch when the URL is called on the mobile device. At the top of your activity you can add in an intent as shown below. You can also do this in the AndroidManifest.xml if you prefer. (Example of xml method in Deep Linking)

 [Activity(Label = "Tesla", MainLauncher = true, Theme = "@style/main", Icon = "@drawable/icon")]
 [IntentFilter(new[] { Android.Content.Intent.ActionView },
                       AutoVerify = true,
                       Categories = new[]
                       {
                            Android.Content.Intent.CategoryDefault,
                            Android.Content.Intent.CategoryBrowsable
                       },
                       DataScheme = "http",
                       DataPathPrefix = "/tesla/",
                       DataHost = "exrin.net")]
 public class MainActivity : FormsAppCompatActivity

In the example above the URL http://exrin.net/tesla will go to this activity. You can create additional Intents to match different patterns to the same Activity. The AutoVerify property you see above you get the Android platform to verify your app is associated and automatically put your app as the default handler for that URL.

Note: If you followed Registering a Uri Scheme, you can have both of these defined at the same time.

Create Your Assets.json

You need to create a file to be placed on your website to help Google prove that you own the website and you are allowing the app to be launched from visiting that URL. Google refers to your assets.json as your digital asset link, that is a public verifiable statement about which page on a website is associated with an app.

You place this file in /.well-known/assetlinks.json on your root domain. Important: This must be served over HTTPS.

The assets.json file looks similar to below.

[{
  "relation": ["delegate_permission/common.handle_all_urls"],
  "target": {
    "namespace": "android_app",
    "package_name": "com.mycompany.myapp",
    "sha256_cert_fingerprints":
    ["16:7E:F1:D1:56:33:06:50:D8:AA:B9:95:F2:43:22:55:16:D0:83:42:1E:1D:B2:A8:8A:04:57:32:3F:CF:44:E5"]
  }
}]

Open your command prompt and go to the directory keytool is in. It comes with the JavaSDK and on my computer was found here.

C:\Program Files (x86)\Java\jdk1.7.0_55\bin\keytool.exe

Now run this command to get your fingerprint.

keytool -list -v -keystore my.keystore

Save the file and upload to your website in the .well-known directory.

Look here if you want more information on Google’s Digital Asset Links.

Test Your Assets.json File

Before you continue on its good to check that your assets.json file is working well and thankfully Google provide a tool to do this. Go to the URL below, replacing the <domain> with your website domain and see if it correctly reads your assets.json file.

https://digitalassetlinks.googleapis.com/v1/statements:list?
 source.web.site=https://<domain>&
 relation=delegate_permission/common.handle_all_urls

Handling App Launch

When your app is launched from a registered URL, we can receive that URL in our Activity and decide how to proceed. In the activity you places

public class MainActivity : FormsAppCompatActivity
{
   protected override void OnCreate(Bundle bundle)
   {
       base.OnCreate(bundle);

       Forms.Init(this, bundle);
 
       // Get the URI information here
       var action = Intent?.Action;
       var data = Intent?.Data;
 
       LoadApplication(new App());

    }
}

Test App Launch

If you want to test your app recognizes the intent correctly and see what data gets passed through you can do that through a command in adb as shown below. Replace my URL with your own.

adb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d "http://exrin.net/tesla"

If you don’t know about adb, its the Android Debug Bridge and in Visual Studio you can launch it by pressing the the button as shown.

adb

 

If you want to delve into more detail have a look at Google’s Handling App Links.

Microsoft MVP (Xamarin) | Exrin | Xamarin Forms Developer | Melbourne, Australia | Open to sponsorship to Canada or US

Related Posts

Leave A Comment?