Working with notifications in Windows Phone 8.1

    Hello to all!

    Today we’ll talk about new features regarding notifications in Windows / Windows Phone 8.1. We will discuss what types of notifications exist, how to organize them in your application, and also note one of the main innovations for Windows Phone devices - the Action Center .


    Notifications in Windows Phone are an important way a user interacts with an application. They help the user learn about updating information in the application, notify him of the actions of other users or remind of the necessary activities.

    Notifications can come from any service or locally from the application.

    In Windows / Windows Phone 8.1, there are several ways to display notifications: A

    user can receive a notification in the form of information on the application tile or using a small black icon.

    You can learn more about the possibilities of living tiles from the next course .

    In addition to this, the following notification methods are available:
    • Toast - notifications - when you receive this type of notification, a text notification appears at the top of the device screen. However, at the moment, the application may be inactive. By clicking on this message, you can quickly switch to the corresponding application.
    • Raw - notifications - with this type of notification, the content is transferred directly to the application. The application processes the contents of the notification according to its own algorithm. These notifications do not have their own UI. Like other push notifications, the WNS service delivers them to the application from the cloud or any other external service.

    Toast - notifications

    To work with Toast notifications, you must enable this method of displaying notifications in the application manifest . We will not be able to send Toast notifications until we indicate that the application can send them.
    Fortunately, it is very simple - switch the corresponding setting in Package.appxmanifest to the “Yes” position: We

    send a Toast notification using the following XML code:
    headline textbody text

    Note that this XML schema is standard for any kind of notifications.
    When you click on Toast - notification, the user will be able to launch the application.

    For the Windows platform, as many as 8 templates for displaying Toast notifications are available, but on Windows Phone 8.1 they have only one fixed view, regardless of the template specified by the developer:

    However, the logic of Toast notifications on both platforms will be the same .

    Ways to implement notifications

    For all Windows devices, there are several ways to implement the notification mechanism:
    • Scheduled notifications. Such notifications will stand in the queue of system tasks and run at a given fixed time;

      How it works:
      Inside the application, the notification scheduler API is used, which refers to the system queue schedule. The system checks the queue and at the time specified for notification sends the update to the tile or Toast.

      var scheduleToast = new ScheduledToastNotification(
                              DateTimeOffset.UtcNow + TimeSpan.FromDays(1.0) );
      var toastNotify = ToastNotificationManager.CreateToastNotifier();

      Using the ScheduledToastNotification class, we create an object responsible for scheduled notification, in which, using the previously demonstrated XML code, specify the content and set the time at which the notification should be displayed. And using the ToastNotificationManager class, create a toastNotify object, for which the function of adding a notification to the AddToSchedule () system queue is implemented.
    • Periodic updates (Periodic) - extract data for notifications from the specified URL and display them with the specified frequency - once per hour / at 6 o’clock / at 12 o’clock;

      How it works:
      In the application, we turn to Windows System Services, indicating that we need notifications with a frequency of 0.5 / 6/12 hours. These services, in turn, are associated with the update API for tiles and icons a certain number of times, using an HTTP request they receive XML notification data that can be passed to a tile or icon.

      var periodic = TileUpdateManager.CreateTileUpdaterForApplication();
      Uri myTileFeed = new Uri("");
      periodic.StartPeriodicUpdate(myTileFeed, PeriodicUpdateRecurrence.Hour);

      Using the TileUpdateManager class, create a periodic object that provides periodic tile updates. Next, create the URI channel myTileFeed, into which the data for notification in xml format will be broadcast. An object of type PeriodicUpdateRecurrence will allow you to set the required frequency of notifications coming from the myTileFeed channel.
      There is an easier way to implement periodic notifications using the settings of the Package.appxmanifest file: You

      only need to select the frequency and specify the channel URI.
    • Local application notifications (Local);

      How it works: The
      application uses tile and icon updates for notifications; in the background, it is already accessing the Toast notification API.

      BadgeNotification newBadge = new BadgeNotification(badgeDoc);
      BadgeUpdater update = BadgeUpdateManager.CreateBadgeUpdaterForApplication();

      We create an icon using an object of type BadgeNotification and the Update () function implemented for an object of type BadgeUpdater, we instantly update the icon.

    Below is a table of the possibility of using methods for implementing notifications, depending on their type:

    We will not disregard the last way to implement notifications - push notifications . Let's consider it in more detail.

    Push notifications

    Prior to the current release, two different services were used for this type of notification in Windows 8.0 and Windows Phone 8.0: Microsoft Push Notification Services (MPNS) for phones and Windows Notification Services (WNS) for other Windows devices.

    However, now Push notifications for any Windows / Windows Phone 8.1 devices are implemented through the WNS service.

    What are the benefits for developers using WNS:
    • The same approach for any device on Windows;
    • Unified application registration process for push notifications;
    • A single push notification template for Windows / Windows Phone 8.1 platforms.

    How it works:
    1. The Windows Phone 8.1 application communicates with the Windows Notification Client platform to obtain the application channel URI, which is unique for each device and application;
    2. Further, the URI channel must be registered in the cloud service;
    3. The cloud service authenticates the WNS service by providing its data (SID packet and secret key), and provides access that allows the service to send notifications. Now you can send push notifications using this service;
    4. The cloud service sends a push notification to the WNS service at the appropriate time;
    5. WNS communicates with the Windows Notification Client platform, which updates any of the specified notification types.

    Read more about the WNS service here .

    Below we implement the mechanism of push notifications in the application step by step, performing a number of necessary actions:

    First, make all the necessary preparations in the Development Center using your developer account:

    Click Submit App and reserve the name of the intended application:

    Fill in App Info: Go down below and in More Options we will find the necessary settings for WNS: Follow the link here : And we get all the necessary data for further work. In Visual Studio, create a test universal application:

    In the solution explorer, right-click on the project for Windows Phone and select Associate App with the Store : The linking wizard starts: Log in using the developer's account and select the application that we registered before: All data is filled in automatically: After clicking on Associate , in The Windows Phone project will create a Package.StoreAssociation.xml file. Log in to the Azure control panel and click the Create button : Click on Application Services -> Service Bus -> Notification Concentrator Click on Quick create

    and fill in the necessary data: Create a new Notification Hub: Click on the newly created namespace TestDemoPushHub-ns and find the newly created notification hub: Go to the settings of the notification hub, where we fill in the required fields noted above: Now the notification hub is configured as a WNS service. The following information will be needed to connect the application to the notification hub: This information can be obtained by clicking on the "Connection Information" button : Connect your application to the notification hub. Right-click on the solution in Visual Studio: In the dialog box

    Manage NuGet Packages, look for WindowsAzure.Messaging.Managed and click Install : Next, select the package installation for the desired project: Now we downloaded, installed and added links to Azure Messaging libraries to all projects using the WindowsAzure.Messaging.Managed NuGet package. Open the App.xaml.cs file from the shared Shared project and connect the following libraries:

    using Windows.Networking.PushNotifications;
    using Microsoft.WindowsAzure.Messaging;
    using Windows.UI.Popups;

    There we add the InitNotifictionAsync method:
    private async void InitNotificationsAsync()
        var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
        var hub = new NotificationHub("TestDemoPushHub", "Endpoint=sb://;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=z2Sj7sgwGpkvTyE/H5QyiffCpwCjV/PmJBY1h4WhXac=");
        var result = await hub.RegisterNativeAsync(channel.Uri);
        // Displays the registration ID so you know it was successful
        if (result.RegistrationId != null)
            var dialog = new MessageDialog("Registration successful: " + result.RegistrationId);
            dialog.Commands.Add(new UICommand("OK"));
            await dialog.ShowAsync();

    This method receives the URI channel for the application from the WNS, then registers this URI channel in the notification hub. When creating the hub variable, do not forget to specify the name of our notification concentrator and the connection string with Listen access:
    var hub = new NotificationHub("", ""); ("TestDemoPushHub", "Endpoint=sb://;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=z2Sj7sgwGpkvTyE/H5QyiffCpwCjV/PmJBY1h4WhXac=");

    Where we get the data for the necessary variables from the Information on connecting the notification concentrator in Azure:

    hub name = TestDemoPush;
    connection string listen access = DefaultListenSharedAccessSignature;

    In the OnLaunched event handler in App.xaml.cs, add a call to the InitNotificationsAsync () method.

    This is necessary so that the URI channel for your notifications is registered every time the application starts.

    In the Solution Explorer, find and configure the Package.appxmanifest file :

    Set Toast capable to Yes.
    Now the application is ready to receive notifications.

    The main function of the application is to show the result of registering the channel: We will

    send notifications to the application.

    Let's create a console application through which we will send push notifications: Click Tools-> NuGet Package Manager-> Package Manager Console Thus, add a link to the Azure Service Bus SDK. We will connect the following library in the project: using Microsoft.ServiceBus.Notifications; And add a method to send notifications:

    private static async void SendNotificationAsync()
        NotificationHubClient hub = NotificationHubClient
            .CreateClientFromConnectionString("", "");
        var toast = @"Hello from a .NET App!";
        await hub.SendWindowsNativeNotificationAsync(toast);

    In the method, create an object of type NotificationHubClient, where we fill in the necessary data for the connection:

    connection string with full access = DefaultFullSharedAccessSignature (your Service Bus);
    hub name = TestDemoPushHub.

    And then we create and fill in the data of the variable itself, which is responsible for the contents of the notification:
    Hello, guys!";

    In the body of the program, we call the written method:
    SendNotificationAsync ();

    We launch and receive push notifications from the TestDemoPush application on the phone:

    Let's look at the notifications in the Notification Center :

    When you click on any of them, we will go to the application.

    Notification center

    A few more words about one of the key innovations in Windows Phone 8.1 - the Notification Center (Action Center). It is called by swipe down from the top of the screen. It gets all new messages, notifications, mentions and reminders from various applications. Also in the Notification Center there are four switches that can be changed to call any settings.

    Now users choose which applications can display notifications in the Notification Center, and application developers have received special APIs that enable them to choose which actions will be available from the notification center. Using these APIs and mechanisms, application developers can manage pop-up notifications.

    New features for developers:
    • Delete one or more notifications;
    • Tagging and notification groups;
    • Replacing an outdated notification with a new one;
    • Setting the duration of the notification;
    • “Goast Toast” - notifications that appear only in the Notification Center.

    The notification center is available only for the phone.

    You can learn more about the capabilities of the Developer Notification Center from this course .


    Notifications in Windows Phone 8.1 are a whole range of various tools that provide the developer with the opportunity to choose the way his application communicates with the user. Nevertheless, I advise you to pay special attention to the incredibly convenient and powerful WNS service, whose advantages were especially noticeable on mobile devices.

    useful links

    Channel 9: Building Apps for Windows Phone 8.1
    Microsoft Azure: Getting Started with Notification Hubs
    MVA: Hosting and Promoting Applications on the Windows Store
    Microsoft Virtual Academy (MVA) Training Courses
    Microsoft Azure Portal
    Windows Development Center
    Free or Trial Visual Studio 2013
    How to Publish an Application on the Windows Store
    Download the sample application from this article

    Also popular now: