I'm showing a notification using Support.V7.NotificationCompat
. I want to navigate to a ViewModel when this notification is clicked. The navigation parameter I am passing to the ViewModel isn't working when I try to show the ViewModel using MvxViewModelRequest
but it works when I use ShowViewModel like so:
ShowViewModel<RouteModificationViewModel>(new { id = existingModification.Id });
Here is the ViewModel Init method. The Guid has the correct value when I navigate using ShowViewModel
. The Guid always has the value Guid.Empty
(all 0's) when I navigate using MvxViewModelRequest
.
public void Init(Guid id)
{
_routeModificationId = id;
}
This is how I am trying to show the ViewModel from the notification:
public void ShowRouteModifiedNotification(RouteModificationModel routeModificationModel)
{
var context = Application.Context;
var builder = new NotificationCompat.Builder(context);
builder.SetDefaults(NotificationCompat.DefaultAll);
builder.SetCategory(NotificationCompat.CategoryStatus);
builder.SetAutoCancel(true);
builder.SetPriority(NotificationCompat.PriorityHigh);
builder.SetSmallIcon(Resource.Mipmap.ic_launcher);
builder.SetContentTitle("Route Modification");
builder.SetContentText("Hello, World!");
builder.SetContentIntent(GetIntent(routeModificationModel.Id));
builder.SetVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
builder.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification));
var notification = builder.Build();
var notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
var handler = new Handler(Looper.MainLooper);
handler.Post(() =>
{
notificationManager.Notify(RouteModificationNotificationId, notification);
});
}
private PendingIntent GetIntent(Guid guid)
{
var request = MvxViewModelRequest<RouteModificationViewModel>.GetDefaultRequest();
var json = Mvx.Resolve<IMvxJsonConverter>().SerializeObject(new { id = guid });
request.PresentationValues = new Dictionary<string, string>
{
{ "id", json }
};
var translator = Mvx.Resolve<IMvxAndroidViewModelRequestTranslator>();
var intent = translator.GetIntentFor(request);
return PendingIntent.GetActivity(Application.Context, 0, intent, 0);
}
Can anyone see what I'm missing or doing wrong?
MvvmCross has since received a major update to the way navigation works in MvvmCross 5.0 and later. This question may only be relevant if you're using MvvmCross 4.x or 3.x. In this case, the MvvmCross navigation API in 4.x was not serializing the Guid correctly. You could work around this problem by serializing the Guid to a string and passing the string as the navigation parameter, then deserializing the string back to a Guid in the ViewModel.