Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin-Android Mvvmcross - Tapping on received notification start app with splash or bring to front

I'm trying to create a neat solution for the following requirement:

a) When the user 'taps' on a notification that my app receives and the app is open and/or in the background, the app will be brought to the font.

b) When the user 'taps' on a notification and the app is closed, the splashscreen shows and the app starts as it would normally would.

I'm trying but I only have success with either one of the above options, not both sadly. This is my code:

    public void CreateNotification(string title, string desc, string pushUrl, string pushTitle)
    {
        var setupSingleton = MvxAndroidSetupSingleton.EnsureSingletonAvailable(ApplicationContext);
        setupSingleton.EnsureInitialized();

        if (!string.IsNullOrWhiteSpace(pushUrl))
        {
            var pushMessageParameterService = Mvx.Resolve<IPushMessageParameterService>();
            pushMessageParameterService.SetPushActionParameters(new PushActionParameters
            {
                UrlToShow = pushUrl,
                ViewTitle = pushTitle
            });
        }

        var intent = new Intent(this, typeof(SplashScreen));
        intent.AddFlags(ActivityFlags.NewTask);
        intent.SetAction(Intent.ActionMain);
        intent.AddCategory(Intent.CategoryLauncher);

        //var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
        //var pendingIntent = PendingIntent.GetActivity(this, 0, intent, 0);
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent.SetFlags(ActivityFlags.BroughtToFront), PendingIntentFlags.CancelCurrent);

        Uri alarmSound = RingtoneManager.GetDefaultUri(RingtoneType.Notification);

        var notificationBuilder = new Notification.Builder(this)
            .SetSmallIcon(Resource.Drawable.Icon)
            .SetContentTitle(title)
            .SetContentText(desc)
            .SetAutoCancel(true)
            .SetSound(alarmSound)
            .SetContentIntent(pendingIntent);

        var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
        Notification notification = notificationBuilder.Build();
        notification.Flags = NotificationFlags.ShowLights | NotificationFlags.AutoCancel;

        notificationManager.Notify(0, notification);
    }

To keep it simple, I've got two activities:

public class SplashScreen : MvxSplashScreenActivity

and

public class DashboardView : BaseMvxActivity

If I use the "SplashScreen" as the PendingIntent for the notification, and the app is already started/open/in the background, it hangs at the splashScreen. The MvvmCross logging shows "Showing ViewModel DashboardViewModel" but stops there. The OnCreate, Init and Start are not called. The Splash just stays.

If I use the "DashboardView" as the PendingIntent for the notification and the app is closed/not active, than I just see a white screen upon start and no splash screen.

I would like to have the best of both worlds :). So when tapping on the push message and the app is open, just bring the app to the front (if it isn't already). And when the app is closed, show the splashscreen etc etc.

I hope I've made my question clear.

Many thanks in advance.

like image 383
JonHendrix Avatar asked Nov 30 '15 15:11

JonHendrix


1 Answers

When I tried to use MvxSplashScreenActivity for my notification intent it froze on that screen.

I made it point to a standard MvxActivity and set the background myself, along with NoHistory = true on the Activity attribute. In the OnCreate in my pending activity I have it start the real intent.

like image 114
RandomEngy Avatar answered Sep 27 '22 22:09

RandomEngy