Search code examples
c#notificationsuwpwindows-10-universalbackground-task

UWP c# app with two kinds of onActivated listeners available - Activation by notification


Currently in my apps i am registering an onActivated listener to be executed and checked for my sharing activity like so:

        Window.Current.Activated += Current_Activated;

with the method as follows to check for the activation state:

private async void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
        {
if (e.WindowActivationState == CoreWindowActivationState.Deactivated)
            {

            }
            else
            {}                       }

Now i would like to implement an activation from a notification from a background task. The click on the notification is currently opening the app or brings it to the foreground if already open. This is standard behaviour. Now I would like to execute some code on activation by the notification but my method never gets called and I am not completely understanding Microsofts online material as it seems relatively simple.

I am registering the listener as such:

        (Application.Current as App).Activated = new EventHandler<IActivatedEventArgs>(App_Activated);

and the method that never gets called looks like this:

private void App_Activated(object sender, IActivatedEventArgs e)
        {                                                        }

Are those two conflicting each other? is there something I can do to implement the notification activation into my existing method?

I've read through this and many more pages: https://blogs.msdn.microsoft.com/tiles_and_toasts/2015/07/08/quickstart-sending-a-local-toast-notification-and-handling-activations-from-it-windows-10/

Even from a test app i downloaded i don't really understand what needs to be done. Besides that i don't need to pass any info from the notification to the main activity. Simply just open it and execute one command of refreshing the page, but only on notification activation.


Solution

  • You should just override the OnActivated(IActivatedEventArgs args) method in your App.xaml.cs and check whether the args.Kind property is ActivationKind.ToastNotification.

    You can find more info about handling an UWP app activation here.