xamarin 深度链接在dotnet maui中关闭Android上的应用程序时不会触发SendOnAppLinkRequestReceived

kmynzznz  于 2023-08-01  发布在  Android
关注(0)|答案(1)|浏览(96)

我正在尝试在iOS和Android的.NET MAUI中实现深度链接。iOS对我有效,但Android只部分有效。
我有一切设置,所以在我的网站上的条目被添加和应用程序得到解雇以及。
我面临的问题是在创建/打开应用程序时从OnCreate调用SendOnAppLinkRequestReceived。
这是我的Android主活动代码。

[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,
    // 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 = "@style/Maui.SplashTheme", 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("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
确实有人在这里提出了同样的解决方案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

    }
    
    // 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<string, object> { { "MyPackage", package } };
        await Shell.Current.GoToAsync($"{nameof(NewPage1)}", navigationParameter);
    }
}

ou6hu8tu

ou6hu8tu1#

此问题在.net8中已解决。我用Preview build测试了一下。https://github.com/dotnet/maui/issues/9658是类似堆栈跟踪的问题。

相关问题