Using Notifications on android with MvvmCross(通过 MvvmCross 在 android 上使用通知)
问题描述
我确实想创建一个插件来实现通知服务之类的功能.
I do want to create a plugin which does implement something like a notification-service.
所以我现在正在做的事情是这样的:
So what I'm doing at the moment is something like this:
var activity = Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity;
var builder = new NotificationCompat.Builder(activity.ApplicationContext)
.SetContentTitle(title)
.SetSmallIcon(Resource.Drawable.Icon)
.SetContentText(message);
var notificationManager = (NotificationManager)activity.ApplicationContext.GetSystemService(Context.NotificationService);
notificationManager.Notify(0, builder.Build());
这很好用,并且确实显示了应显示的通知.下一步是,我想从通知导航到我的活动.这意味着在 MvvmCross 中我确实想导航到我的 ViewModel.
This works fine and does show the notification as it should show. Next step is, that I want to navigate from the notification to my activity. Which means in MvvmCross I do want to navigate to my ViewModel.
但是我现在如何将 ShowViewModel<...>()-Command 打包到此通知中?这甚至可能吗?
But how do I now pack the ShowViewModel<...>()-Command into this notification? Is this even possible?
在原生 android 上,我会创建一个 PendingIntent,它确实指向我想要显示的 Activity.
On native android I would create a PendingIntent which does point to my Activity I want to show.
有什么想法吗?暗示?小费?:-)
Any idea? Hint? Tip? :-)
推荐答案
Android 上默认的 MvvmCross Presenter 使用 Intent 进行导航.这些是由IMvxAndroidViewModelRequestTranslator接口中的Intent GetIntentFor(MvxViewModelRequest request)方法生成的.
The default MvvmCross presenter on Android uses Intents for navigation. These are generated by the method Intent GetIntentFor(MvxViewModelRequest request) in the IMvxAndroidViewModelRequestTranslator interface.
默认情况下,这是在以下位置实现的:MvxAndroidViewsContainer.cs#L117
By default this is implemented within: MvxAndroidViewsContainer.cs#L117
public virtual Intent GetIntentFor(MvxViewModelRequest request)
{
var viewType = GetViewType(request.ViewModelType);
if (viewType == null)
{
throw new MvxException("View Type not found for " + request.ViewModelType);
}
var converter = Mvx.Resolve<IMvxNavigationSerializer>();
var requestText = converter.Serializer.SerializeObject(request);
var intent = new Intent(_applicationContext, viewType);
intent.PutExtra(ExtrasKey, requestText);
AdjustIntentForPresentation(intent, request);
intent.AddFlags(ActivityFlags.NewTask);
return intent;
}
如果您需要生成 Intent 用于其他目的(例如,为了继续生成 PendingIntent),那么您可以 Resolve 并自己调用这个接口.
If you need to generate Intents for other purposes (e.g. in order to then go on and generate PendingIntents) then you can Resolve and call this interface yourself.
var request = MvxViewModelRequest<MyViewModel>.GetDefaultRequest();
request.PresentationValues = new Dictionary<string, string>() {
{ "life", "42" }
};
var translator = Mvx.Resolve<IMvxAndroidViewModelRequestTranslator>();
var intent = translator.GetIntentFor(request);
var pending = PendingIntent.GetActivity (context, 0, intent, 0);
<小时>
有关生成 MvxViewModelRequest 对象的更多信息,另请参阅 ShowViewModel 方法/v3/Cirrious/Cirrious.MvvmCross/ViewModels/MvxNavigatingObject.cs">MvxNavigatingObject.cs
For further information on generating MvxViewModelRequest objects, see also the overloaded ShowViewModel methods in MvxNavigatingObject.cs
这篇关于通过 MvvmCross 在 android 上使用通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:通过 MvvmCross 在 android 上使用通知
基础教程推荐
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
