App Links for Android with Rivits

How to Link to other apps and Handle Incoming links using App Links

PDF for offline use

Let us know how you feel about this

Translation Quality


0/250

last updated: 2017-02

This recipe will show you how to link to other apps using the Rivets component from the Xamarin Component Store, as well as register your app to receive and parse incoming App Links.

Overview

This recipe will show you how to link to other apps using the Rivets component from the Xamarin Component Store, as well as register your app to receive and parse incoming App Links.

There are two main parts to implementing App Links in your apps

  1. Linking to other apps - Instead of opening a web link in a browser directly, you should let the App Links navigation take care of how to open a link
  2. Handling incoming links from other apps - You can advertise on web page links App Link metadata which tells other apps how to deep link to the same content in your app.

Linking to other apps

  • Install the Rivets component from the Component Store.
  • Find any instances where you navigate to a URL in your app (either by starting an Activity with an Intent.ActionView Intent or by loading the page into a custom web view) and replace them with this:
Rivets.AppLinks.Navigator.Navigate("http://any.old.url")
  • Your app will now attempt to Navigate to another installed app for the URL using App Links, or will fall back to using an intent with a view action if no App Link meta data is found, or no apps for the metadata are installed.

Handling incoming App Links

  • Install the Rivets component from the Component Store if you have not already done so.

  • Create the following Layout Resource named ProductLayout.axml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp">
    <TextView
        android:text="Product ID:"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1" />
    <TextView
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textViewProductId" />
</LinearLayout>
  • Add the following Activity to your app:
[Activity (Label = "Product Details")]
[IntentFilter(new [] {Android.Content.Intent.ActionView },
    DataScheme="example",
    DataHost="products",
    Categories=new [] { Android.Content.Intent.CategoryDefault, Android.Content.Intent.CategoryBrowsable })]
public class ProductActivity : Activity
{
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        SetContentView (Resource.Layout.ProductLayout);

        var id = "No Product ID Found";

        if (Intent.HasExtra ("al_applink_data")) {

            var appLinkData = Intent.GetStringExtra ("al_applink_data");

            var alUrl = new Rivets.AppLinkUrl (Intent.Data.ToString (), appLinkData);

            // InputQueryParameters will contain our product id
            if (alUrl != null && alUrl.InputQueryParameters.ContainsKey ("id"))
                id = alUrl.InputQueryParameters ["id"];
        }

        FindViewById<TextView> (Resource.Id.textViewProductId).Text = id;
    }
}
  • When your app is opened with the url example://products?id=12345 the product Activity will be displayed and you should see the id 12345 from the query string displayed.

  • Create an HTML page with the following HTML:

<html>
 <head>
  <title>Product 12345</title>
  <meta property="al:android:url" content="example://products?id=12345" />
 </head>
 <body>
  <h1>Product 12345</h1>
  <p>This could be a page about Product 12345</p>
 </body>
</html>
  • Publish this page somewhere on the internet, or at a location your Android app can reach.

  • Test navigating using App Links from your Android app by calling this method somewhere:

Rivets.AppLinks.Navigator.Navigate("http://location/of/your/html/file.html");

Summary

This recipe will show you how to link to other apps using App Links, as well as register your app to receive and parse incoming App Links.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.