Using Notifications on android with MvvmCross

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:

  • Use different theme depending on if device is android tablet or phone
  • Changing from free app to paid app on Google Play
  • Android get date before 7 days (one week)
  • How to get Android Studio to recognize file as source (test)
  • Gradle 4.0 Unable to find a matching configuration
  • How to finish an activity from another activity
  •         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());
    

    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.

    But how do I now pack the ShowViewModel<…>()-Command into this notification? Is this even possible?

    On native android I would create a PendingIntent which does point to my Activity I want to show.

    Any idea? Hint? Tip? 🙂

    Related posts:

    Launching an intent for file and MIME type?
    How can I create an AttributeSet from a style.xml?
    Using SVG files with libgdx
    How can I Zip and Unzip a string using GZIPOutputStream that is compatible with .Net?
    RecyclerView Swipe with a view below it
    Customize Edittext input character to image?
  • Compile and link against libusb for android
  • Android development environment in Docker
  • How can Android service update the UI of the activity that started it?
  • “You cannot install this app because another user has already installed an incompatible version on this device”
  • Android: Kotlin with Butterknife
  • How to know whether a RecyclerView / LinearLayoutManager is scrolled to top or bottom?
  • One Solution collect form web for “Using Notifications on android with MvvmCross”

    The default MvvmCross presenter on Android uses Intents for navigation. These are generated by the method Intent GetIntentFor(MvxViewModelRequest request) in the IMvxAndroidViewModelRequestTranslator interface.

    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;
        }
    

    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);
    

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

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.