gpt4 book ai didi

c# - 如何使用带有自定义应用程序启动的 MvvmCross 从推送通知启动关闭的应用程序

转载 作者:行者123 更新时间:2023-11-30 16:38:44 28 4
gpt4 key购买 nike

我正在使用 Xamarin 和 MvvmCross 为 iOS 和 Android 平台开发一款手机应用。当应用程序处于前台和后台时,我可以接收推送通知。

当应用程序关闭时,我也会收到推送通知,当我点击它们时,应用程序会启动,但我无法区分应用程序启动是因为用户单击了应用程序图标还是因为推送通知点击。当应用程序关闭并通过推送通知启动时,我想导航到具体的 View 模型。

我找到了这篇文章,它提出了一个我还没有实现的解决方案,它可以解决平台相关代码 (iOS) 中的问题:

https://theconfuzedsourcecode.wordpress.com/2015/09/22/handling-push-notifications-in-xamarin-ios-when-app-is-closed/

同样指向这个方向的是 xamarin 论坛中的这个答案:

https://forums.xamarin.com/discussion/67698/ios-notifications-while-app-is-closed

If the user taps on the banner displayed, the >AppDelegate.FinishedLaunching method is called. The NSDictionary of >options passed in will contain >UIApplication.LaunchOptionsRemoteNotificationKey to let you know the >app was launched because of a push notification. You can check for this >and handle it accordingly for your app. i.e. Display an alert, navigate >to a page, etc.

但我的问题是,如何在不同平台共享的核心项目中做到这一点?我已按照 MvvmCross 文档中本文末尾显示的步骤进行操作:

https://www.mvvmcross.com/documentation/advanced/customizing-appstart

所以我的代码如下所示:

应用.cs

public class App : MvxApplication<object>
{
public override void Initialize()
{
CreatableTypes().EndingWith("Service").AsInterfaces().RegisterAsLazySingleton();
Mvx.IoCProvider.LazyConstructAndRegisterSingleton<IAppStartSynchronizer, AppStartSynchronizer>();
Mvx.IoCProvider.RegisterSingleton(() => UserDialogs.Instance);

RegisterCustomAppStart<HaasSohnAppStart<AppSyncViewModel>>();
}

public override object Startup(object parameter)
{
//never reached
Console.WriteLine("APP.CS STARTUP");
return base.Startup(parameter);
}
}

自定义应用启动

public class HaasSohnAppStart<AppSyncViewModel> : MvxAppStart<AppSyncViewModel, object> where AppSyncViewModel : IMvxViewModel<object>
{
public HaasSohnAppStart(IMvxApplication application, IMvxNavigationService navigationService) : base(application, navigationService){}

/// <summary>
/// Don't await the navigation to the first view model to be able to do async operations in the first <typeparamref name="AppSyncViewModel"/>
/// see: https://nicksnettravels.builttoroam.com/post/2018/04/19/MvvmCross-Initialize-method-on-the-first-view-model.aspx
/// see: https://stackoverflow.com/questions/49865041/mvvmcross-app-wont-start-after-upgrading-to-version-6-0
/// see: https://github.com/MvvmCross/MvvmCross/issues/2829
/// </summary>
/// <param name="hint">Hint.</param>
protected override Task NavigateToFirstViewModel(object hint)
{
//First of all configure app synchronously to have db connection, api urls, translations, etc configured
AppConfig.Initialize().GetAwaiter().GetResult();
NavigationService.Navigate<AppSyncViewModel, object>(hint);
return Task.CompletedTask;
}

protected override Task<object> ApplicationStartup(object hint = null)
{
//hint always null
Console.WriteLine($"APPSTART APPLICATION STARTUP HINT = {hint}");
return base.ApplicationStartup(hint);
}
}

第一个 View 模型的摘录

public class AppSyncViewModel : BaseViewModel, IMvxViewModel<object>
{
private IAppStartSynchronizer _appSyncrhronizer;
private INetworkAvailable _networkAvailable;
private MainController _mainController = MainController.Instance;
override public string Title => Strings["AppSyncViewModel_Label_Title"];

public string SyncInfo { get; set; } = "AppSyncViewModel_Label_SyncInProgress".Translate();

private bool _comeFromPushNotification;
public AppSyncViewModel(IMvxNavigationService navigationService, IAppStartSynchronizer appSynchronizer, INetworkAvailable networkAvailable, IUserDialogs userDialogs) : base(navigationService, userDialogs)
{
_appSyncrhronizer = appSynchronizer;
_networkAvailable = networkAvailable;
}

public override async Task Initialize()
{
if (_appSyncrhronizer != null)
{
IsBusy = true;
_appSyncrhronizer.SyncFinish += Syncnronizer_SyncFinish;
await _appSyncrhronizer.StartupSync(_networkAvailable);
}

await base.Initialize();
}

public async void Syncnronizer_SyncFinish(object sender, System.EventArgs e)
{
if (_comeFromPushNotification)
{
await _userDialogs.AlertAsync("we come from push notification", "parameter");
}
}

public void Prepare(object parameter)
{
//parameter always null
_comeFromPushNotification = parameter != null ? true : false;
Console.WriteLine($"APPSYNCVIEWMODEL: parameter = {parameter}");
}

我希望此实现在 NavigateToFirstViewModel 方法中的自定义应用启动类的提示参数中接收一些参数,然后在第一个 View 模型 (AppSyncViewModel) 的准备方法中设置一个标志以了解我们是否从推送通知,具体取决于此参数。

但是,结果是该参数始终为 null,即使应用已关闭并从推送通知启动也是如此。

因此,总结一下我的问题,在使用 MvvmCross 的核心项目中,我如何检测应用程序是从推送通知启动的?

最佳答案

所以你为提示返回 null 的原因是因为 MvvmCross 在 MvxApplicationDelegate 中将传入的对象覆盖为 null。

因此,当您在 iOS 上点击推送通知时,将调用 FinishedLaunching 方法,并使用填充有 LaunchOptionsRemoteNotificationKey 的 NSDictionary 参数。发生的情况是此键被覆盖为 null 而不是传递。

您可以在 MvvmCross 源代码中看到这种情况 here

所以你解决这个问题的方法是在你的 ApplicationDelegate 类中,覆盖 GetAppStartHint 并像这样实现它:

protected override object GetAppStartHint(object hint = null)
{
return hint;
}

这将返回传递给方法的参数,而不是 null。我不确定为什么 MvvmCross 在这里将参数覆盖为 null 而不是只传递提示参数。

关于c# - 如何使用带有自定义应用程序启动的 MvvmCross 从推送通知启动关闭的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54593528/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com