0

I am doing push notifications in Windows 10 universal using C# and XAML. Everything is working perfectly. When I receive a notification and the app is running I use this function to do something when the notification comes in.

private async void Channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs e)
{
    e.Cancel = true;
    //do something in reaction to the notification
}

But when the app is in the background I get a toast notification in the form of a badge. When I click that the app opens to the last page it was on. However what I want to do is when the badge is clicked I want to be able to call a function in my code. How do I do this?

Bart
  • 9,925
  • 7
  • 47
  • 64

2 Answers2

2

If your app is not running in the front, it will be activated and you can put your code in the OnActivated event handler.

Depending on the type of notification you'll use, there are small differences in handling, possible types are:

  • Foreground activation from a toast notification using Windows 10 adaptive template
  • Background activation from a toast notification using Windows 10 adaptive template
  • Legacy: Foreground activation from a toast notification using legacy template.

This is the code you'll be using for foreground activation with the Windows 10 toast templates (most used one):

protected override void OnActivated(IActivatedEventArgs args)
{
    // TODO: Initialize root frame just like in OnLaunched

    // Handle toast activation
    if (args.Kind == ActivationKind.ToastNotification)
    {
        var toastArgs = args as ToastNotificationActivatedEventArgs;

        // your code
    }

    // TODO: Handle other types of activation
}

You can follow this quickstart for sending and handling activations for a sample of each type. The quickstart also uses a NuGet package to make your life easier.

Bart
  • 9,925
  • 7
  • 47
  • 64
0

You need to add windows runtime component in your project to handle and process notification in background while your app is closed. Here is the quickstart for handling different kind of triggers using windows runtime component.

here is the video tutorial by lynda for the same

JD-V
  • 3,336
  • 1
  • 17
  • 20