Skip to content

Latest commit

 

History

History
92 lines (74 loc) · 6.91 KB

File metadata and controls

92 lines (74 loc) · 6.91 KB

Notifications on Windows Template Studio

❗ There is also a version of this document with code samples in VB.Net

Windows Template Studio supports three different type of notifications for UWP:

  • Toast notifications
  • Hub notifications
  • Store notifications

Toast notifications

ToastNotificationService is in change of sending notifications directly from code in the application. The service contains a method ShowToastNotification that sends a notification to the Windows action center. The feature code also includes a ShowToastNotificationSample class that shows how to create and send a notification from code.

ToastNotificationsService extends ActivationHandler to handle application activation from a toast notification. This code is not implemented on the template because the logic is application dependent. The following ToastNotificationSample contains an example of one way to handle application activation from a toast notification. Check out the activation documentation to learn more about handling app activation. The relevant parts of the sample app that handle activation are shown below.

// ToastNotificationService.cs
protected override async Task HandleInternalAsync(ToastNotificationActivatedEventArgs args)
{
    // Handle the app activation from a ToastNotification
    NavigationService.Navigate<Views.ActivatedFromToastPage>(args);
    await Task.CompletedTask;
}

// ActivatedFromToastPage.xaml.cs
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    ViewModel.Initialize(e.Parameter as ToastNotificationActivatedEventArgs);
}

// ActivatedFromToastViewModel.cs
public void Initialize(ToastNotificationActivatedEventArgs args)
{
    // Check args looking for information about the toast notification
    if (args.Argument == "ToastButtonActivationArguments")
    {
        // ToastNotification was clicked on OK Button
        ActivationSource = "ActivationSourceButtonOk".GetLocalized();
    }
    else if(args.Argument == "ToastContentActivationParams")
    {
        // ToastNotification was clicked on main content
        ActivationSource = "ActivationSourceContent".GetLocalized();
    }
}

Full toast notification documentation for UWP here.

Hub notifications

HubNotificationsService is in change of configuring the application with the Azure notifications service to allow the application to receive push notifications from a remote service in Azure. The service contains the InitializeAsync method that sets up the Hub Notifications. You must specify the hub name and the access signature before start working with Hub Notifications. There is more documentation about how to create and connect an Azure notifications service here.

Toast Notifications sent from Azure notification service shoudl be handled in the same way as locally generated ones. See the above referenced ToastNotificationSample for more.

Store notifications

StoreNotificationsService is in change of configuring the application with the Windows Dev Center notifications service to allow the application to receive push notifications from Windows Dev Center remote service. The service contains the InitializeAsync method that sets up the Store Notifications. This feature use the Store API to configure the notifications.

See the official documentation on how to configure your app for targeted push notifications and how to send notifications to your app's customers.

To handle app activation from a Toast Notification that is sent from Windows Dev Center service will need you to add a similar implementation to that detailed above. The key difference is to call ParseArgumentsAndTrackAppLaunch to notify the Windows Dev Center that the app was launched in response to a targeted push notification and to get the original arguments. An example of this is shown below.

protected override async Task HandleInternalAsync(ToastNotificationActivatedEventArgs args)
{
    var toastActivationArgs = args as ToastNotificationActivatedEventArgs;

    StoreServicesEngagementManager engagementManager = StoreServicesEngagementManager.GetDefault();
    string originalArgs = engagementManager.ParseArgumentsAndTrackAppLaunch(toastActivationArgs.Argument);

    //// Use the originalArgs variable to access the original arguments passed to the app.
    NavigationService.Navigate<Views.ActivatedFromStorePage>(originalArgs);

    await Task.CompletedTask;
}

Other interesting links about notifications