如何使用 MvvmCross 5 IMvxNavigationService 获取 PendingIntent?
How do I get PendingIntent using the MvvmCross 5 IMvxNavigationService?
我有一个在 MvvmCross 4.x 中使用的方法,它与 NotificationCompat.Builder
一起使用来设置通知的 PendingIntent
以在通知被点击时显示 ViewModel用户。我正在尝试将此方法转换为使用 MvvmCross 5.x IMvxNavigationService
但看不到如何设置演示参数,并使用新导航 [=25= 获取 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);
}
当我单击通知时确实出现了 RouteNotificationViewModel,但未调用 Prepare
和 Initialize
。将此方法从 MvvmCross 4.x 导航样式转换为 MvvmCross 5.x 导航样式需要什么?
可以在 MvvmCross 5+ 中执行此操作,但它不像以前那样干净。
对于初学者,您想为 activity 指定 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.
}
}
我有一个在 MvvmCross 4.x 中使用的方法,它与 NotificationCompat.Builder
一起使用来设置通知的 PendingIntent
以在通知被点击时显示 ViewModel用户。我正在尝试将此方法转换为使用 MvvmCross 5.x IMvxNavigationService
但看不到如何设置演示参数,并使用新导航 [=25= 获取 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);
}
当我单击通知时确实出现了 RouteNotificationViewModel,但未调用 Prepare
和 Initialize
。将此方法从 MvvmCross 4.x 导航样式转换为 MvvmCross 5.x 导航样式需要什么?
可以在 MvvmCross 5+ 中执行此操作,但它不像以前那样干净。
对于初学者,您想为 activity 指定 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.
}
}