显示mvvmcross中Notification / PendingIntent的片段

时间:2014-03-25 04:00:56

标签: android mvvmcross

在下面的示例中,基于mvvmcross的Android应用程序是从Notification / PendingIntent打开的。 PendingIntent的目标实际上是一个MvxFragment。

当点击通知时,应用程序中没有任何反应,相关的ViewModel构造函数未被触发,也没有记录MVX事件。如果PendingIntent的目标被更改为活动派生,那么一切都正常。

正确的MVX'处理这种情况的方法,即通知的目标是一个片段。

示例损坏的代码:

        var appContext = Mvx.Resolve<IMvxAndroidGlobals>().ApplicationContext;

        // SomeViewModel  --- derives MvxViewModel
        // SomeViewModelView --- front end MvxFragment for SomeViewModel 
        var request = new MvxViewModelRequest<SomeViewModel>(
            new MvxBundle(SomeViewModel.CreateParameters("a_parameter_value").ToSimplePropertyDictionary()),
            null,
            null);

        var translator = Mvx.Resolve<IMvxAndroidViewModelRequestTranslator>();
        var uiIntent = translator.GetIntentFor(request);
        var pendingUiIntent = PendingIntent.GetActivity(appContext, 0, uiIntent, 0);

        var notificationManager = (NotificationManager)appContext.GetSystemService(Context.NotificationService);

        var notificationBuilder = new NotificationCompat.Builder(context)
            .SetAutoCancel(true)
        ...
            .SetContentIntent(onSelectedIntent);

        // show the notification
        notificationManager.Notify(id, notificationBuilder.Build());


        // after user taps notification, nothing happens

1 个答案:

答案 0 :(得分:0)

我猜您的代码在GetIntentFor阶段的某个地方失败了 - 您不能简单地请求片段Intent - 导航Intent只对{{Activity有用1}}秒。我的猜测是您的代码实际上失败了https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Droid/Views/MvxAndroidViewsContainer.cs#L127

引发的异常
  

正确的'MVX'处理这样的场景的方法是什么,即Notification的目标是一个片段。

您的问题没有标准答案,无论是在MvvmCross还是在香草Android中。

但你不能只发送一个Intent来展示Fragment - Android就不会那样。

您需要确定应用在响应此通知时需要执行的操作 - 是否需要切换到新的Activity来托管Fragment,还是需要显示现有Fragment中的Activity。一旦你完成了这项工作,你就可以找出你想要展示的MvvmCross ViewModel和/或你希望代码采取什么行动。

在这里看到有很多类似问题的Java问题 - 它们可能有所帮助: - Launch a fragment in my Android application from the notification bar - Fragments and Notifications: Target different Activities from Notification; depending on screen configuration