【问题标题】:How do I get PendingIntent using the MvvmCross 5 IMvxNavigationService?如何使用 MvvmCross 5 IMvxNavigationService 获得 PendingIntent?
【发布时间】:2017-10-05 21:29:44
【问题描述】:

我有一个我在 MvvmCross 4.x 中使用的方法,它与NotificationCompat.Builder 一起使用来设置通知的PendingIntent,以便在用户单击通知时显示 ViewModel。我正在尝试将此方法转换为使用 MvvmCross 5.x IMvxNavigationService,但看不到如何设置演示参数,并使用新的导航 API 获取 PendingIntent

private PendingIntent RouteNotificationViewModelPendingIntent(int controlNumber, RouteNotificationContext notificationContext, string stopType)
{
    var request = MvxViewModelRequest<RouteNotificationViewModel>.GetDefaultRequest();
    request.ParameterValues = new Dictionary<string, string>
    {
        { "controlNumber", controlNumber.ToString() },
        { "notificationContext", notificationContext.ToString() },
        { "stopType", stopType }
    };
    var translator = Mvx.Resolve<IMvxAndroidViewModelRequestTranslator>();
    var intent = translator.GetIntentFor(request);
    intent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);

    return PendingIntent.GetActivity(Application.Context,
                                     _notificationId,
                                     intent,
                                     PendingIntentFlags.UpdateCurrent);
}

当我单击通知但未调用 PrepareInitialize 时,确实会出现 RouteNotificationViewModel。将此方法从 MvvmCross 4.x 样式的导航转换为 MvvmCross 5.x 样式的导航需要什么?

【问题讨论】:

  • 你能添加你用来接收意图的代码吗?
  • 没有接收到意图的代码。当我单击通知时,Android 会创建活动。该部分正在工作,但是当 Android 显示活动时,MvvmCross 不会调用 PrepareInitialize。假设导航参数是手动序列化的,这一切都在 MvvmCross 4 中起作用。现在在 MvvmCross 5 中,导航参数的序列化方式不同。
  • 你能在 repo 中为此打开一个问题吗?恐怕目前 v5 Navigation 不支持这种情况。但我们正在积极开发 MvxNavigationService。
  • Here's GitHub 问题,供任何关注此问题的人使用。

标签: c# xamarin.android mvvmcross


【解决方案1】:

在 MvvmCross 5+ 中可以做到这一点,但它不像以前那样干净。

对于初学者,您想为您的活动指定 singleTop 启动模式:

[Activity(LaunchMode = LaunchMode.SingleTop, ...)]
public class MainActivity : MvxAppCompatActivity

像这样生成通知PendingIntent:

var intent = new Intent(Context, typeof(MainActivity));
intent.AddFlags(ActivityFlags.SingleTop);
// Putting an extra in the Intent to pass data to the MainActivity
intent.PutExtra("from_notification", true);
var pendingIntent = PendingIntent.GetActivity(Context, notificationId, intent, 0);

现在有两个地方可以处理来自 MainActivity 的这个 Intent,同时仍然允许使用 MvvmCross 导航服务:

如果点击通知时应用程序未运行,则将调用OnCreate

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    if (bundle == null && Intent.HasExtra("from_notification"))
    {
        // The notification was clicked while the app was not running. 
        // Calling MvxNavigationService multiple times in a row here won't always work as expected. Use a Task.Delay(), Handler.Post(), or even an MvvmCross custom presentation hint to make it work as needed.
    }
}

如果在单击通知时应用正在运行,则将调用 OnNewIntent

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);
    if (intent.HasExtra("from_notification"))
    {
        // The notification was clicked while the app was already running.
        // Back stack is already setup.
        // Show a new fragment using MvxNavigationService.
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多