Jason Yang
Jason Yang

Reputation: 130

UWP C# - Re-launch app from notification click

I'm writing a UWP app, and I have a ScheduledToastNotification that is added to the schedule when the app is suspended (e.g. like a reminder). However, if I close the app, the notification appears on time, but when I click on the notification (no buttons, just on the notification in general), the app doesn't launch correctly, stopping at the splash screen.

How do I get the app the re-launch correctly?

Thanks.

Upvotes: 3

Views: 2386

Answers (1)

GeralexGR
GeralexGR

Reputation: 3582

You should override OnActivated in App.Xaml.cs and handle this like

protected override void OnActivated(IActivatedEventArgs args)
        {
            if (args.Kind == ActivationKind.ToastNotification)
            {
                var toastArgs = args as ToastNotificationActivatedEventArgs;
                var arguments = toastArgs.Argument;

                if (arguments == "ARG")
                {
                    Frame rootFrame = Window.Current.Content as Frame;
                    if (rootFrame == null)
                    {
                        rootFrame = new Frame();
                        Window.Current.Content = rootFrame;
                    }
                    rootFrame.Navigate(typeof(YOURPAGE));
                    Window.Current.Activate();
                }
            }
        }

Upvotes: 7

Related Questions