当应用程序关闭并收到通知时,如何在通知服务中初始化 MvvmCross? [英] How is MvvmCross initialized in a notification service when app is closed and notification is received?

查看:69
本文介绍了当应用程序关闭并收到通知时,如何在通知服务中初始化 MvvmCross?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中创建了一个服务来监听来自 GCM 的推送通知.

I've created a service in my application that listens for push notifications from GCM.

服务的 OnMessage 方法在我测试过的所有场景中都被正确调用:应用程序在前台运行、应用程序在后台运行和应用程序未运行.但是,当应用程序未运行时,当我尝试使用 Mvx.Resolve 解析依赖项时,代码会抛出 NullReferenceException.

The service's OnMessage method gets called correctly in all scenarios that I've tested: app running in foreground, app running in background and app not running. However, when the app is not running, the code is throwing a NullReferenceException when I try to resolve dependencies with Mvx.Resolve.

Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
  at Cirrious.CrossCore.Mvx.Resolve[IRepository] () [0x00000] in <filename unknown>:0 
  at MyApp.Android.Notifications.GcmService+<OnMessage>d__3.MoveNext () [0x00000] in <filename unknown>:0 
Process my.app.droid (pid 7846) has died.

看来,由于应用程序未运行,Setup 类尚未执行,并且 IoC 容器不知道如何解析我的类型.或者,更糟糕的是,Mvx 类根本没有初始化.

It appears that, since the app isn't running, the Setup class has not been executed and the IoC container doesn't know how to resolve my types. Or, even worse, the Mvx class has not been initialized and at all.

我似乎无法在网络上找到有关如何解决此问题的文档.

I can't seem to find documentation on the web on how to work around this.

我是否不走运,必须尝试使用​​通知中发送的信息启动我的应用程序,并且仅在应用程序完全启动后才对我的 IRepository 执行操作?

Am I out of luck and have to try to launch my application with what information was sent in the notification and only perform the operations on my IRepository once the app has fully launched?

或者有没有办法确保 Mvx 在应用程序关闭时运行的服务被系统激活时正确初始化?当然,这将是更好的选择,因为我可能会在其他后台服务中遇到这种情况,这些服务监听互联网访问更改或位置更改.

Or is there a way to ensure that Mvx is properly initialised when a service running while the app is closed is activated by the system? This, of course, would be the better option, since I'm likely to encounter this in other background services listening to internet access changes or location changes.

推荐答案

Stuart Lodge 的评论让我在正确跟踪并帮助我解决了我的问题,但我会亲自为可能遇到相同问题的其他人详细回答这个问题.

Stuart Lodge's comment put me on the right track and helped my solve my problem, but I'll answer the question in detail myself for others who might have the same issue.

我从网上的不同文档中没有理解的是,MvvmCross Android 应用程序是通过 MvxAndroidSetupSingleton 初始化的.这种类型的方法,据我所知,可以安全地重复调用以确保单例存在并正确初始化:

What I hadn't understood from the different documents online, is that an MvvmCross Android app is initialized through a MvxAndroidSetupSingleton. This type has methods that, as far as I understand, can safely be called repeatedly to ensure that the singleton exists and is properly initialized:

var setupSingleton = MvxAndroidSetupSingleton.EnsureSingletonAvailable(context);
setupSingleton.EnsureInitialized();

那时我所要做的就是在通知服务上收到消息时调用此代码:

All I had to do then, was call this code when a message is received on the notification service:

protected async override void OnMessage(Context context, Intent intent)
{
    var setupSingleton = MvxAndroidSetupSingleton.EnsureSingletonAvailable(context);
    setupSingleton.EnsureInitialized();

    await MyMobileApp.EnsureDataStoreInitialized();

    var logger = Mvx.Resolve<ILogger>();
    logger.Send(LogLevel.Info, "Notification received");
}

如果应用程序正在运行,它基本上是一个空操作,但如果应用程序被关闭,它将初始化 MvvmCross 运行 Setup 类的方法等.

If the app was running, it's basically a no-op, but if the app was closed, it will initialize MvvmCross running the Setup class's methods, etc.

然后我借用了这个概念并在我的应用程序上实现了一个确保"类型的方法,该方法将执行我通常在启动画面中执行的部分初始化(主要是确保数据存储存在).

I then borrowed the concept and implemented an "Ensure"-type method on my app that will do part of the initialization that I normally do in my splash screen (mainly, ensuring that the data store exists).

初始化 MvxAndroidSetupSingleton 后,我可以像往常一样解析依赖项.

Having initialized the MvxAndroidSetupSingleton, I can then resolve dependencies as usual.

这篇关于当应用程序关闭并收到通知时,如何在通知服务中初始化 MvvmCross?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆