Android Deep Linking (Intents) Support in .NET MAUI

Devin Michael 21 Reputation points
2022-06-20T16:02:40.277+00:00

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.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,900 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,656 Reputation points Microsoft Vendor
    2022-06-21T09:48:28.423+00:00

    Hello,​

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

    Here is a solution, you can handle this Intent in the OnCreate method of MainActivity like following code and verified the Intent is an Action.View and strLink is not empty.

       protected override void OnCreate(Bundle savedInstanceState)  
           {  
               base.OnCreate(savedInstanceState);  
         
                   Intent intent = this.Intent;             
                   var action = intent.Action;  
                   var strLink = intent.DataString;  
                   if (Intent.ActionView == action && !string.IsNullOrWhiteSpace(strLink))  
                   {  
                        
                       Toast.MakeText(this, strLink, ToastLength.Short).Show();  
                   }  
         
                
           }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful