如何使用带有自定义应用程序启动的 MvvmCross 从推送通知启动关闭的应用程序
How to flag if closed app started from push notification using MvvmCross with custom app start
我正在使用 Xamarin 和 MvvmCross 为 iOS 和 Android 平台开发手机应用程序。当应用程序处于前台和后台时,我可以接收推送通知。
当应用程序关闭时,我也收到推送通知,当我点击它们时,应用程序启动了,但我不知道应用程序启动是因为用户点击了应用程序图标还是因为推送通知被点击。我想在应用程序关闭并通过推送通知启动时导航到具体视图模型。
我找到了这篇文章,它提出了一个我还没有实现的解决方案,它可以解决平台相关代码中的问题 (iOS):
同样指向这个方向,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
所以我的代码如下所示:
App.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);
}
}
自定义 AppStart
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:
/// 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);
}
}
第一个视图模型的摘录
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 方法中接收自定义应用启动的提示参数中的一些参数 class 然后在第一个视图模型 (AppSyncViewModel) 的准备方法中设置一个标志以了解如果我们根据此参数从推送通知打开应用程序。
不过,结果是该参数始终为 null,即使应用程序关闭并从推送通知启动也是如此。
所以,总结一下我的问题,我如何在使用 MvvmCross 的核心项目中检测到该应用程序是从推送通知启动的?
所以你返回 null 的原因是因为 MvvmCross 在 MvxApplicationDelegate 中将传入的对象覆盖为 null。
因此,当您点击 iOS 上的推送通知时,会调用 FinishedLaunching 方法,并使用填充有 LaunchOptionsRemoteNotificationKey 的 NSDictionary 参数。发生的情况是此键被覆盖为 null 而不是传递。
您可以在 MvvmCross 源代码中看到这一点 here
所以您解决这个问题的方法是在您的 ApplicationDelegate class 中,覆盖 GetAppStartHint 并像这样实现它:
protected override object GetAppStartHint(object hint = null)
{
return hint;
}
这将 return 传递给方法的参数而不是 null。我不确定为什么 MvvmCross 在这里将参数覆盖为 null 而不是仅通过 hint 参数。
我正在使用 Xamarin 和 MvvmCross 为 iOS 和 Android 平台开发手机应用程序。当应用程序处于前台和后台时,我可以接收推送通知。
当应用程序关闭时,我也收到推送通知,当我点击它们时,应用程序启动了,但我不知道应用程序启动是因为用户点击了应用程序图标还是因为推送通知被点击。我想在应用程序关闭并通过推送通知启动时导航到具体视图模型。
我找到了这篇文章,它提出了一个我还没有实现的解决方案,它可以解决平台相关代码中的问题 (iOS):
同样指向这个方向,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
所以我的代码如下所示:
App.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);
}
}
自定义 AppStart
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:
/// 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);
}
}
第一个视图模型的摘录
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 方法中接收自定义应用启动的提示参数中的一些参数 class 然后在第一个视图模型 (AppSyncViewModel) 的准备方法中设置一个标志以了解如果我们根据此参数从推送通知打开应用程序。
不过,结果是该参数始终为 null,即使应用程序关闭并从推送通知启动也是如此。
所以,总结一下我的问题,我如何在使用 MvvmCross 的核心项目中检测到该应用程序是从推送通知启动的?
所以你返回 null 的原因是因为 MvvmCross 在 MvxApplicationDelegate 中将传入的对象覆盖为 null。
因此,当您点击 iOS 上的推送通知时,会调用 FinishedLaunching 方法,并使用填充有 LaunchOptionsRemoteNotificationKey 的 NSDictionary 参数。发生的情况是此键被覆盖为 null 而不是传递。
您可以在 MvvmCross 源代码中看到这一点 here
所以您解决这个问题的方法是在您的 ApplicationDelegate class 中,覆盖 GetAppStartHint 并像这样实现它:
protected override object GetAppStartHint(object hint = null)
{
return hint;
}
这将 return 传递给方法的参数而不是 null。我不确定为什么 MvvmCross 在这里将参数覆盖为 null 而不是仅通过 hint 参数。