Deep linking doesn't fire SendOnAppLinkRequestReceived when app closed on Android in dotnet maui

huangapple go评论106阅读模式
英文:

Deep linking doesn't fire SendOnAppLinkRequestReceived when app closed on Android in dotnet maui

问题

我正在尝试在.NET MAUI中为iOS和Android实现深度链接。iOS对我有效,但Android只能部分地工作。

我已经设置好了一切,因此我的网站条目已添加,应用程序也已启动。

我遇到的问题是在创建/打开应用程序时,从OnCreate调用SendOnAppLinkRequestReceived的问题。

这是我在Android上的MainActivity代码。

[IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
    AutoVerify = true, DataScheme = "test-app123")]
[IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
    AutoVerify = true, DataScheme = "https", DataHost = "test.app123.app")]
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true,
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density,
    LaunchMode = LaunchMode.SingleTask
)]
public class MainActivity : MauiAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        OnNewIntent(Intent);
    }

    protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);
        string action = intent.Action;
        string strLink = intent.DataString;

        if (Intent.ActionView != action || string.IsNullOrWhiteSpace(strLink))
        {
            Console.WriteLine("Empty");
            return;
        }

        var link = new Uri(strLink);
        Console.WriteLine("Link " + link);
        App.Current.SendOnAppLinkRequestReceived(link);
    }
}

如果应用程序在后台运行,然后尝试使用链接打开它,我的应用程序会按预期工作。然后会调用OnNewIntent,然后调用SendOnAppLinkRequestReceived。

但当应用程序关闭时,它不会调用SendOnAppLinkRequestReceived,而是给我一个错误。

android.runtime.JavaProxyThrowable: System.Reflection.TargetInvocationException: Arg_TargetInvocationException
                                     ---> System.InvalidOperationException: MauiContext should have been set on parent.

这发生在我在OnCreate中调用OnNewIntent时。但如果我不这样做,我怎么能调用SendOnAppLinkRequestReceived。

有人在这里建议了相同的解决方案https://stackoverflow.com/questions/72686426/android-deep-linking-intents-support-in-net-maui,该解决方案已被接受,但对我不起作用。

我的App.Xaml.cs看起来像这样:

public App()
{
    InitializeComponent();

    MainPage = new AppShell();
#if IOS
    (Application.Current as IApplicationController)?.SetAppIndexingProvider(new Microsoft.Maui.Controls.Compatibility.Platform.iOS.IOSAppIndexingProvider());
#endif
#if ANDROID
    (Application.Current as IApplicationController)?.SetAppIndexingProvider(new 
Microsoft.Maui.Controls.Compatibility.Platform.Android.AndroidAppIndexProvider(Android.App.Application.Context));
#endif

#if IOS || ANDROID

    try
    {
        var entry = new AppLinkEntry
        {
            Title = "Find Shop",
            Description = "Find Shop Deep Link",
            AppLinkUri = new Uri("https://test.app123.app/", UriKind.RelativeOrAbsolute),
            IsLinkActive = true
        };
        Application.Current.AppLinks.RegisterLink(entry);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Crash");
    }
#endif

}

protected async override void OnAppLinkRequestReceived(Uri uri)
{
    Console.WriteLine(uri.ToString());
    Package package = new Package() { packageUri = uri };

    var navigationParameter = new Dictionary<string, object> { { "MyPackage", package } };
    await Shell.Current.GoToAsync($"{nameof(NewPage1)}", navigationParameter);
}
}
英文:

I am trying to implement Deep linking in .NET MAUI for iOS and Android. iOS works for me but Android only works partially.

I have everything setup, so the entries in my website is added and the app gets fired as well.

The issue I am facing is to Invoke SendOnAppLinkRequestReceived from OnCreate, when the app is created/opened.

this is my MainActivity code for Android.


[IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
    AutoVerify = true, DataScheme = &quot;test-app123&quot;)]
[IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
    AutoVerify = true, DataScheme = &quot;https&quot;, DataHost = &quot;test.app123.app&quot;)]
[Activity(Theme = &quot;@style/Maui.SplashTheme&quot;, MainLauncher = true,
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density,
    // Prevents MainActivitiy from being re-created on launching an intent (also makes it to where `OnNewIntent` will be called directly, if the app has already been loaded)
    LaunchMode = LaunchMode.SingleTask
)]
//[Activity(Theme = &quot;@style/Maui.SplashTheme&quot;, MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        
        OnNewIntent(Intent);

    }

    protected override void OnNewIntent(Intent intent)
    {
      
        base.OnNewIntent(intent);
        
        string action = intent.Action;
        string strLink = intent.DataString;
       
        if (Intent.ActionView != action || string.IsNullOrWhiteSpace(strLink))
        {
            Console.WriteLine(&quot;Empty&quot;);
            return;
        }
        

        var link = new Uri(strLink);
        Console.WriteLine(&quot;Link &quot; + link);
        App.Current.SendOnAppLinkRequestReceived(link);
    }

}

My app works as expected if the app is running in background and then try opening it using the link. the OnNewIntent gets called then the SendOnAppLinkRequestReceived.

But when the app is closed it doesn't call the SendOnAppLinkRequestReceived and instead gives me a error.

 android.runtime.JavaProxyThrowable: System.Reflection.TargetInvocationException: Arg_TargetInvocationException
                                                                                                     ---&gt; System.InvalidOperationException: MauiContext should have been set on parent.

This occurs when I call OnNewIntent in my OnCreate. But if I don't how can I invoke the SendOnAppLinkRequestReceived

Someone did suggest the same solution here https://stackoverflow.com/questions/72686426/android-deep-linking-intents-support-in-net-maui which is accepted but just doesnt work for me.

My App.Xaml.cs looks something like this:

public App()
{
		InitializeComponent();
		

		MainPage = new AppShell();
#if IOS
        (Application.Current as IApplicationController)?.SetAppIndexingProvider(new Microsoft.Maui.Controls.Compatibility.Platform.iOS.IOSAppIndexingProvider());
#endif
#if ANDROID
        (Application.Current as IApplicationController)?.SetAppIndexingProvider(new 
Microsoft.Maui.Controls.Compatibility.Platform.Android.AndroidAppIndexProvider(Android.App.Application.Context));
#endif
       
#if IOS || ANDROID

        try
        {
            var entry = new AppLinkEntry
            {
                Title = &quot;Find Shop&quot;,
                Description = &quot;Find Shop Deep Link&quot;,
                
                
                AppLinkUri = new Uri(&quot;https://test.app123.app/&quot;, UriKind.RelativeOrAbsolute),
                IsLinkActive = true
            };
            Application.Current.AppLinks.RegisterLink(entry);
        }
        catch (Exception ex)
        {
            Console.WriteLine(&quot;Crash&quot;);
        }
#endif

    }
    
    // Package: a model class with a property packageUri (type Uri)
    protected async override void OnAppLinkRequestReceived(Uri uri)
    {
		Console.WriteLine(uri.ToString());
        Package package = new Package() { packageUri = uri };

        var navigationParameter = new Dictionary&lt;string, object&gt; { { &quot;MyPackage&quot;, package } };
        await Shell.Current.GoToAsync($&quot;{nameof(NewPage1)}&quot;, navigationParameter);
    }
}

答案1

得分: 1

问题已在.net8中解决。我使用预览版本进行了测试。
https://github.com/dotnet/maui/issues/9658 是一个具有类似堆栈跟踪的问题。

英文:

The issue is resolved in .net8. I tested it out using the preview build.
https://github.com/dotnet/maui/issues/9658 was the issue with a similar stack trace.

huangapple
  • 本文由 发表于 2023年7月11日 12:19:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定