通过 MvvmCross 在 android 上使用通知 [英] Using Notifications on android with MvvmCross

查看:28
本文介绍了通过 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 对象的更多信息,另请参阅 MvxNavigatingObject.cs


For further information on generating MvxViewModelRequest objects, see also the overloaded ShowViewModel methods in MvxNavigatingObject.cs

这篇关于通过 MvvmCross 在 android 上使用通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆