Live Push Notifications Sample for Windows 8 Metro LOB Apps


It’s worth to mention that integrating Push Notifications(Badges, Tiles, Toast Notifications) on Windows 8 Metro LOB apps are indispensible while maintaining live streaming notifications from RSS feed / backend database systems. Badges, Live Tiles, Toast Notifications are integral part of a Metro Business apps (connected with backend Dynamics CRM, Dynamics AX, SQL Azure Feed, Live RSS).

  • In order to implement Live Tiles Notifications & Toast notifications , you need to incorporate standard XML codes as follows:

public overridestring GetContent()

{

if (RequireSquareContent && SquareContent == null)

{

throw new NotificationContentValidationException(“Square tile content should be included with each wide tile. “ +

“If this behavior is undesired, use the RequireSquareContent property.”);

}

     StringBuilder visualNode = newStringBuilder(String.Empty);

visualNode.AppendFormat(“<visual version='{0}'”, Util.NOTIFICATION_CONTENT_VERSION);

    if (!String.IsNullOrWhiteSpace(Lang))

{

visualNode.AppendFormat(” lang='{0}'”, Util.HttpEncode(Lang));

}

   if (Branding != TileBranding.Logo)

{

visualNode.AppendFormat(” branding='{0}'”, Branding.ToString().ToLowerInvariant());

}

   if (!String.IsNullOrWhiteSpace(BaseUri))

{

visualNode.AppendFormat(” baseUri='{0}'”, Util.HttpEncode(BaseUri));

}

visualNode.Append(“>”);

     StringBuilder builder = newStringBuilder(String.Empty);

builder.AppendFormat(“<tile>{0}<binding template='{1}’>{2}</binding>”, visualNode, TemplateName, SerializeProperties(Lang, BaseUri));

if (SquareContent != null)

{

ISquareTileInternal squareBase = SquareContent asISquareTileInternal;

if (squareBase == null)

{

throw new NotificationContentValidationException(“The provided square tile content class is unsupported.”);

}

builder.Append(squareBase.SerializeBinding(Lang, BaseUri, Branding));

}

builder.Append(“</visual></tile>”);

return builder.ToString();

}

private ISquareTileNotificationContent  m_SquareContent = null;

private bool m_RequireSquareContent = true;

}

  • Put standard logo & Badges sample in Assets folder of your Metro apps & enable Toast Notification on Package.appexmanifest.

  • Put some standard codes on your MainPage.xaml.cs in order to enable Live Text & images update, Tile update, Toast Notification & Badges support.

using  System;

using System.Collections.Generic;

using  System.IO;

using  System.Linq;

using  Windows.Foundation;

using  Windows.Foundation.Collections;

using   Windows.UI.Xaml;

using   Windows.UI.Xaml.Controls;

using  Windows.UI.Xaml.Controls.Primitives;

using  Windows.UI.Xaml.Data;

using  Windows.UI.Xaml.Input;

using  Windows.UI.Xaml.Media;

using  Windows.UI.Xaml.Navigation;

using  NotificationsExtensions.BadgeContent;

using  NotificationsExtensions.TileContent;

using  NotificationsExtensions.ToastContent;

using  Windows.Data.Xml.Dom;

using  Windows.UI.Notifications;

using  Windows.UI.StartScreen;

using  Windows.ApplicationModel.DataTransfer.ShareTarget;

using   Windows.ApplicationModel.DataTransfer;

namespace LiveTiles

{

///<summary>

/// An empty page that can be used on its own or navigated to within a Frame.

///</summary>

public sealed partial class MainPage : Page

{

privateDataTransferManager _dataTransferManager;

public MainPage()

{

this.InitializeComponent();

}

///<summary>

/// Invoked when this page is about to be displayed in a Frame.

///</summary>

///<param name=”e”>Event data that describes how this page was reached.  The Parameter

/// property is typically used to configure the page.</param>

///

privatevoid RegisterForShare()

{

DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();

dataTransferManager.DataRequested += newTypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.MainPage_DataRequested);

}

privatevoid MainPage_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)

{

DataRequest request = args.Request;

request.Data.Properties.Title =

“Sharing Toast Notification”;

request.Data.Properties.Description =“A demonstration on Share Contracts”;

request.Data.SetText(“Toast Notification”);

}

protected override void OnNavigatedTo(NavigationEventArgs e)

{

}

void UpdateTileWithText(string text)

{

XmlDocument tileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWideText03);

XmlNodeList textElements = tileXml.GetElementsByTagName(“text”);

textElements.Item(0).AppendChild(tileXml.CreateTextNode(text));

TileNotification tile = newTileNotification(tileXml);

TileUpdateManager.CreateTileUpdaterForApplication().Update(tile);

}

privatevoid UpdateTextButton_Click_1(object sender, RoutedEventArgs e)

{

UpdateTileWithText(“Windows 8 is the Future of Metro Apps”);

}

private void UpdateTextAndImageButton_Click_1(object sender, RoutedEventArgs e)

{

UpdateTileWithImage(“Windows 8 LOB Apps”, http://icons.iconarchive.com/icons/dakirby309/windows-8-metro/256/Apps-Messaging-alt-Metro-icon.png&#8221;);

}

privatevoid UpdateTextWithExtensionsButton_Click_1(object sender, RoutedEventArgs e)

{

UpdateTileWithTextExtensions(“Windows 8 App Developer Blog: Live Tiles”);

}

private void UpdateTextAndImageWithExtensionsButton_Click_1(object sender, RoutedEventArgs e)

{

UpdateTextAndImageExtensions(“Windows 8 Metro Apps Developer Blog: Live Notifications” , http://www.stealthsettings.com/wp-content/uploads/2012/03/Windows-8-Start-Page-Manage-Apps.jpg&#8221;);

}

privatevoid CreateBadge_Click_1(object sender, RoutedEventArgs e)

{

UpdateBadgeWithNumber(3);

}

private void CreateToastNotification_Click_1(object sender, RoutedEventArgs e)

{

CreateToast();

}

void UpdateTileWithImage(string text, string imageSrc)

{

XmlDocument tileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWideImageAndText01);

XmlNodeList textElements = tileXml.GetElementsByTagName(“text”);

textElements.Item(0).AppendChild(tileXml.CreateTextNode(text));

XmlNodeList imageElements = tileXml.GetElementsByTagName(“image”);

XmlElement imageElement = (XmlElement)imageElements.Item(0);

imageElement.SetAttribute(“src”, imageSrc);

imageElement.SetAttribute(“alt”, “Image description”);

XmlDocument squareTileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquareImage);

XmlNodeList squareImageElements = squareTileXml.GetElementsByTagName(“image”);

XmlElement squareImageElement = (XmlElement)squareImageElements.Item(0);

squareImageElement.SetAttribute(“src”, imageSrc);

squareImageElement.SetAttribute(“alt”, “Image description”);

IXmlNode subnode = tileXml.ImportNode(squareTileXml.GetElementsByTagName(“binding”).Item(0), true);

tileXml.GetElementsByTagName(“visual”).Item(0).AppendChild(subnode);

TileNotification tile = newTileNotification(tileXml);

TileUpdateManager.CreateTileUpdaterForApplication().Update(tile);

}

void UpdateTileWithTextExtensions(string text)

{

ITileWideText03 tileContent = TileContentFactory.CreateTileWideText03();

tileContent.TextHeadingWrap.Text = text;

ITileSquareText04 squareContent = TileContentFactory.CreateTileSquareText04();

squareContent.TextBodyWrap.Text = text;

tileContent.SquareContent = squareContent;

TileUpdateManager.CreateTileUpdaterForApplication().Update(tileContent.CreateNotification());

}

void UpdateTextAndImageExtensions(string text, string imageSrc)

{

ITileWideImageAndText01 tileContent = TileContentFactory.CreateTileWideImageAndText01();

tileContent.TextCaptionWrap.Text = text;

tileContent.Image.Src = imageSrc;

tileContent.Image.Alt = “Web Image”;

ITileSquareImage squareContent = TileContentFactory.CreateTileSquareImage();

squareContent.Image.Src = imageSrc;

squareContent.Image.Alt = “Web Image”;

tileContent.SquareContent = squareContent;

TileUpdateManager.CreateTileUpdaterForApplication().Update(tileContent.CreateNotification());

}

private async void CreateSecondaryTile_Click_1(object sender, RoutedEventArgs e)

{

Uri logo = newUri(“ms-appx:///assets/small.png”);

Uri smallLogo = newUri(“ms-appx:///assets/secondary.png”);

string tileActivationArguments = “timeTileWasPinned=” + DateTime.Now.ToLocalTime().ToString();

//Create a 1×1 secondary tile

SecondaryTile secondaryTile = newSecondaryTile(“AppSecondaryTile”

“Windows 8 Enterprise”

“”,

tileActivationArguments,

TileOptions.ShowNameOnLogo,    smallLogo);

secondaryTile.ForegroundText = ForegroundText.Light;

bool isPinned = await secondaryTile.RequestCreateForSelectionAsync(GetElementRect((FrameworkElement)sender), Windows.UI.Popups.Placement.Right);

}

void UpdateBadgeWithNumber(int number)

{

BadgeNumericNotificationContent badgeContent = newBadgeNumericNotificationContent((uint)number);

BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badgeContent.CreateNotification());

}

Rect GetElementRect(FrameworkElement element)

{

GeneralTransform buttonTransform = element.TransformToVisual(null);

Point point = buttonTransform.TransformPoint(newPoint());

return new Rect(point, newSize(element.ActualWidth, element.ActualHeight));

}

private  void CreateToast()

{

  IToastNotificationContent toastContent = null;

IToastText01 templateContent = ToastContentFactory.CreateToastText01();

templateContent.TextBodyWrap.Text = “Windows 8 Metro Apps jump start!”;

toastContent = templateContent;

ToastNotification toast = toastContent.CreateNotification();

ToastNotificationManager.CreateToastNotifier().Show(toast);

}

}

}

Check the Toast Notifications on Live Tiles as follows:

  • Update Text & Images on Live Tiles with Toast Notification as follows:

  • Update Live Badges with updated text as follows:

  • Update images with Live Tiles as follows:

  • More information on Live Tiles & App badges Sample is available here.
Advertisements

About Anindita
Anindita Basak is working as Big Data Cloud Consultant in Microsoft. Worked in multiple MNCs as Developer & Senior Developer on Microsoft Azure, Data Platform, IoT & BI , Data Visualization, Data warehousing & ETL & of course in Hadoop platform.She played both as FTE & v- employee in Azure platform teams of Microsoft.Passionate about .NET , Java, Python & Data Science. She is also an active Big Data & Cloud Trainer & would love share her experience in IT Training Industry. She is an author, forum contributor, blogger & technical reviewer of various books on Big Data Hadoop, HDInsight, IoT & Data Science, SQL Server PDW & PowerBI.

2 Responses to Live Push Notifications Sample for Windows 8 Metro LOB Apps

  1. Great example … Thanks for sharing…

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: