Quickstart: Using the NotificationsExtensions library in your code (XAML)

Note  Not using C#/VB/C++? See Quickstart: Using the NotificationsExtensions library in your code (HTML).

 

The NotificationsExtensions object model library allows you to supply tile, badge, and toast notification XML template contents without using the XML Document Object Model (DOM). It gives you a simpler, clearer, and more direct method of supplying notification content. When you use this library in Microsoft Visual Studio, you gain these advantages:

  • IntelliSense which lists the available tags and attributes as object properties, rather than having to consult the schema documentation for elements and structure.
  • A factory function through which you can create skeleton notifications that you can then fill in with your content.
  • A simplified way to include multiple tile sizes in your notification payload (a best practice!).
  • A helpful way to find and fill in text and image attributes, which are named to give you more information as to their intended use, size, or position in the template.

Note  The NotificationsExtensions library is released under an Microsoft Limited Public License (MS-LPL). You may reuse the library in your Windows Store app or web service and customize it if necessary for your app.

 

Prerequisites

Instructions

1. Get the NotificationsExtensions library

The NotificationsExtensions library is included in several downloadable tile, toast, and notifications samples and can be copied from them for your own use. We'll use the main tiles and badges sample in this procedure.

Note  NotificationsExtensions is a C# library, but can be included in and used with JavaScript, C#, C++, and Visual Basic projects.

 

  1. Download the App tiles and badges sample from the Windows Dev Center.
  2. Unzip the "App tiles and badges sample.zip" file to a folder of your choosing.
  3. Go to the folder to which you unzipped the sample. Copy the NotificationsExtensions folder from the sample into your own project directory, as a sibling of your project's .sln file.

2. Include the library in your project

  1. Launch Visual Studio and open your project.
  2. In the Project Explorer, right-click the solution at the top of the tree.
  3. Select Add then Existing Project.
  4. Navigate to the NotificationsExtensions folder in your project and select the NotificationsExtensions.csproj file.
  5. In each project within your solution, right-click References and select Add Reference.
  6. In the Reference Manager, under Solution, select NotificationsExtensions and then click OK. At this point, NotificationsExtensions is ready to use.

Note  If you don't want to include the full NotificationsExtensions project in your solution, you can build NotificationsExtensions as a stand-alone project and simply include the NotificationsExtensions.winmd as a reference in your project.

 

3. Include the library on your app server

You can also use NotificationsExtensions in your app server code if you are using ASP.NET to send push notifications to Windows Push Notification Services (WNS) or if you are implementing an ASP.NET service to handle requests for periodic tile or badge updates. The only caveat is that you must add the WINRT_NOT_PRESENT compilation build symbol in the NotificationsExtensions project properties.

  1. Go to the directory in which you unzipped the sample. Open the sample folder and copy its NotificationsExtensions folder to a new location of your choosing.

  2. Select the NotificationsExtensions.csproj file to open the project in Visual Studio.

  3. From the Project menu, choose NotificationsExtensions Properties.

  4. Change the Output type selection to "Class Library" to build a DLL file.

  5. Also in the NotificationsExtensions properties, select the Build tab.

  6. Under the General category, add "WINRT_NOT_PRESENT" in the Conditional compilation symbols box. If the box already contains other symbols, add a semi-colon as a separator before "WINRT_NOT_PRESENT".

  7. Press F7 or use Build > Build Solution to build the project.

  8. Copy the built NotificationsExtensions.dll file from your folder (under \NotificationsExtensions\bin\Debug\) to your app server code.

4. Use the library in your code

NotificationsExtensions can now be used as an object in your code. Its contains three members of note:

  • BadgeContent
  • TileContent
  • ToastContent

Each of those content types in turn contains members that represent the elements and attributes for each type.

The following example uses NotificationsExtensions to assign a value to a numeric badge, and then sends it to the tile.

using Windows.UI.Notifications;
using NotificationsExtensions.BadgeContent;

BadgeNumericNotificationContent badgeContent = new BadgeNumericNotificationContent((uint)85);
BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badgeContent.CreateNotification());
using namespace Windows::UI::Notifications;
using namespace NotificationsExtensions::BadgeContent;

auto badgeContent = ref new BadgeNumericNotificationContent(85);
BadgeUpdateManager::CreateBadgeUpdaterForApplication()->Update(badgeContent->CreateNotification());

For comparison, the following example shows the same C# procedure without NotificationsExtensions, using the XML DOM.

using Windows.UI.Notifications;
using Windows.Data.Xml.Dom;

XmlDocument badgeXml = BadgeUpdateManager.GetTemplateContent(BadgeTemplateType.BadgeNumber);
XmlElement badgeElement = (XmlElement)badgeXml.SelectSingleNode("/badge");
badgeElement.SetAttribute("value", "85");

BadgeNotification badge = new BadgeNotification(badgeXml);
BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badge);

The next example uses NotificationsExtensions to supply text in a tile notification. The first step uses the NotificationsExtensions TileContentFactory function to create an object based on a specific template. There is a separate create function for each template; simply attach the word "create" to the template name, such as "createTileWide310x150ImageAndText01".

The template-based object then provides IntelliSense to show the elements available in that template, as properties named according to their function, such as textHeading, textBodyWrap, textBody1, or textColumn1Row4. This tells you precisely which element you are assigning.

Once you've assigned values to the properties, you send the tile notification normally.

using Windows.UI.Notifications;
using NotificationsExtensions.TileContent;

ITileSquare150x150Text01 tileContent = TileContentFactory.CreateTileSquare150x150Text01();

tileContent.TextHeading.Text = "Hello!";
tileContent.TextBody1.Text = "One";
tileContent.TextBody2.Text = "Two";
tileContent.TextBody3.Text = "Three";

TileUpdateManager.CreateTileUpdaterForApplication().Update(tileContent.CreateNotification());
using namespace Windows::UI::Notifications;
using namespace NotificationsExtensions::TileContent;

auto tileContent = TileContentFactory::CreateTileSquare150x150Text01();

tileContent->TextHeading->Text = "Hello!";
tileContent->TextBody1->Text = "One";
tileContent->TextBody2->Text = "Two";
tileContent->TextBody3->Text = "Three";

TileUpdateManager::CreateTileUpdaterForApplication()->Update(tileContent->CreateNotification());

This example uses NotificationsExtentions to assign text and an image to a notification. Like text, image elements are shown as properties named for their function in the template, such as imageMain and imageSmallColumn2Row2.

using Windows.UI.Notifications;
using NotificationsExtensions.TileContent;

ITileWide310x150ImageAndText01 tileContent = TileContentFactory.CreateTileWide310x150ImageAndText01();

tileContent.TextCaptionWrap.Text = "This tile notification uses ms-appx images";

tileContent.Image.Src = "ms-appx:///images/redWide.png";
tileContent.Image.Alt = "A red rectangle";
using namespace Windows::UI::Notifications;
using namespace NotificationsExtensions::TileContent;

auto tileContent = TileContentFactory::CreateTileWide310x150ImageAndText01();

tileContent->TextCaptionWrap->Text = "This tile notification uses ms-appx images";

tileContent->Image->Src = "ms-appx:///images/redWide.png";
tileContent->Image->Alt = "A red rectangle";

This example uses NotificationsExtensions to define a full tile notification. This illustrates one peculiarity of this library—the order in which you add template sizes to the payload matters.

  • A small template (square71x71—Windows Phone 8.1 only) can only be added to a medium template.
  • A medium template (square150x150) can only be added to a wide template.
  • A wide template can only be added to a large (square310x310—Windows only) template.

If you try to do it in a different order, you'll find that the necessary properties are not available.

using Windows.UI.Notifications;
using NotificationsExtensions.TileContent;
                        
ITileSquare310x310Text09 tileLargeContent = TileContentFactory.CreateTileSquare310x310Text09();

tileLargeContent.TextHeadingWrap.Text = "Lorem ipsum dolor sit amet, consectetur";
tileLargeContent.TextHeading1.Text = "Lorem ipsumo";
tileLargeContent.TextHeading2.Text = "Lorem ipsumo";
tileLargeContent.TextBody1.Text = "VivAmus tincidunt convallis urn";
tileLargeContent.TextBody2.Text = "VivAmus tincidunt convallis urn";

ITileWide310x150Text04 tileWideContent = TileContentFactory.CreateTileWide310x150Text04();
tileWideContent.TextBodyWrap.Text = "Lorem ipsum dolor sit amet, consectetur";

ITileSquare150x150Block tileMediumContent = TileContentFactory.CreateTileSquare150x150Block();
tileMediumContent.TextBlock.Text = "24";
tileMediumContent.TextSubBlock.Text = "Aliquam";
        
tileWideContent.Square150x150Content = tileMediumContent;
tileLargeContent.Wide310x150Content = tileWideContent;

TileUpdateManager.CreateTileUpdaterForApplication().Update(tileLargeContent.CreateNotification());
using namespace Windows::UI::Notifications;
using namespace NotificationsExtensions::TileContent;
                        
auto tileLargeContent = TileContentFactory::CreateTileSquare310x310Text09();

tileLargeContent->TextHeadingWrap->Text = "Lorem ipsum dolor sit amet, consectetur";
tileLargeContent->TextHeading1->Text = "Lorem ipsumo";
tileLargeContent->TextHeading2->Text = "Lorem ipsumo";
tileLargeContent->TextBody1->Text = "VivAmus tincidunt convallis urn";
tileLargeContent->TextBody2->Text = "VivAmus tincidunt convallis urn";

auto tileWideContent = TileContentFactory::CreateTileWide310x150Text04();
tileWideContent->TextBodyWrap->Text = "Lorem ipsum dolor sit amet, consectetur";

auto tileMediumContent = TileContentFactory::CreateTileSquare150x150Block();
tileMediumContent->TextBlock->Text = "24";
tileMediumContent->TextSubBlock->Text = "Aliquam";

tileWideContent->Square150x150Content = tileMediumContent;
tileLargeContent->Wide310x150Content = tileWideContent;

TileUpdateManager::CreateTileUpdaterForApplication()->Update(tileLargeContent->CreateNotification());

This example uses NotificationsExtensions to supply content for a toast notification, including text, image, audio, duration, and launch parameters.

IToastImageAndText01 templateContent = ToastContentFactory.CreateToastImageAndText01();
                templateContent.TextBodyWrap.Text = "Body text that wraps";
                templateContent.Image.Src = TOAST_IMAGE_SRC;
                templateContent.Image.Alt = ALT_TEXT;                        
                        
                        
IToastNotificationContent toastContent = NotificationsExtensions.ToastContent.ToastContentFactory.CreateToastImageAndText01();

toastContent.TextBodyWrap.Text = "Lorem ipsum dolor sit amet";

toastContent.Image.Src = "ms-appx:///images/redWide.png";
toastContent.Image.Alt = "A red rectangle";

toastContent.Audio.Content = NotificationsExtensions.ToastContent.ToastAudioContent.LoopingAlarm;
toastContent.Audio.Loop = true;

toastContent.Duration = NotificationsExtensions.ToastContent.ToastDuration.Long;

toastContent.Launch = "{\"type\":\"toast\",\"param1\":\"12345\",\"param2\":\"67890\"}";

ToastNotification toast = toastContent.CreateNotification();
Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier().Show(toast);
IToastImageAndText01^ toastContent = NotificationsExtensions::ToastContent::ToastContentFactory::CreateToastImageAndText01();

toastContent->TextBodyWrap->Text = "Lorem ipsum dolor sit amet";

toastContent->Image->Src = "ms-appx:///images/redWide.png";
toastContent->Image->Alt = "A red rectangle";

toastContent->Audio->Content = NotificationsExtensions::ToastContent::ToastAudioContent::LoopingAlarm;
toastContent->Audio->Loop = true;

toastContent->Duration = NotificationsExtensions::ToastContent::ToastDuration::Long;

toastContent->Launch = "{\"type\":\"toast\",\"param1\":\"12345\",\"param2\":\"67890\"}";

auto toast = toastContent->CreateNotification();

Windows::UI::Notifications::ToastNotificationManager::CreateToastNotifier()->Show(toast);

Summary

This topic has shown you how to use NotificationsExtensions to simplify the creation of badge, tile, and toast notifications. This is a more straightforward way to populate a template than dealing directly with the XML DOM.