Home > Software engineering >  Android Deep Linking (Intents) Support in .NET MAUI
Android Deep Linking (Intents) Support in .NET MAUI

Time:06-21

I am currently trying to add deep linking support (via Intents) to an Android application written using .NET MAUI.

I have added an activity XML element under the application element in my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true">
      <activity android:name="TestApp.MainActivity" android:exported="true">
        <intent-filter>
          <action android:name="android.intent.action.VIEW" />
          <category android:name="android.intent.category.DEFAULT" />
          <category android:name="android.intent.category.BROWSABLE" />
          <data android:scheme="https"
                android:host="test"
                android:pathPrefix="/group" />
        </intent-filter>
      </activity>
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

I have also added an IntentFilter to the MainActivity.cs under Platforms\Android (see below):

[IntentFilter(new[] { Intent.ActionView },
    Categories = new[]
    {
        Intent.ActionView,
        Intent.CategoryDefault,
        Intent.CategoryBrowsable
    },
    DataScheme = "https", DataHost = "test", DataPathPrefix = "/group"
    )
]
public class MainActivity : MauiAppCompatActivity

Just not sure what to do at this point to react (where to put event handler, etc.) to the intent.

If anyone has any suggestions, it would be greatly appreciated.

CodePudding user response:

You can handle the intent by overriding OnNewIntent method .

Fetch the information from intent.DataString and do what you want.

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        //test
        OnNewIntent(Intent);
    }

    protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);

        var data = intent.DataString;

        if (intent.Action != Intent.ActionView) return;
        if (string.IsNullOrWhiteSpace(data)) return;

        if (data.Contains("/group"))
        {
            //do what you want 
        }
    }
  • Related