Last active
December 11, 2015 20:28
-
-
Save clemensv/4655204 to your computer and use it in GitHub Desktop.
Service Bus Notification Hubs with Windows 8 and Templates. Toasts and Live Tile. Either or both. With peek and pictures. One input message.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async Task InitNotificationsAsync() | |
{ | |
await notificationHub.RefreshRegistrationsAsync(); | |
if (!await notificationHub.RegistrationExistsForApplicationAsync("myUrgentToast")) | |
{ | |
await notificationHub.CreateTemplateRegistrationForApplicationAsync( | |
CreateToastTemplate(), "myUrgentToast", new string[]{"urgent", "alert"}); | |
} | |
if (!await notificationHub.RegistrationExistsForApplicationAsync("myAppTile")) | |
{ | |
await notificationHub.CreateTemplateRegistrationForApplicationAsync( | |
CreateTileTemplate(), "myAppTile", new string[] { "urgent", "all" }); | |
} | |
} | |
XmlDocument CreateToastTemplate() | |
{ | |
var t = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02); | |
ApplyExpressionToXmlNode(t, "//text[@id='1']", "$(headline)"); | |
ApplyExpressionToXmlNode(t, "//text[@id='2']", "$(message)"); | |
return t; | |
} | |
XmlDocument CreateTileTemplate() | |
{ | |
var t = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWidePeekImage05); | |
ApplyExpressionToXmlNode(t, "//image[@id='1']/@src", "$(bigimg)"); // <- wide tile image URL (resource or hosted) | |
ApplyExpressionToXmlNode(t, "//image[@id='2']/@src", "$(smlimg)"); // <- small image URL (resource or hosted) | |
ApplyExpressionToXmlNode(t, "//text[@id='1']", "$(headline)"); | |
ApplyExpressionToXmlNode(t, "//text[@id='2']", "$(message)"); | |
return t; | |
} | |
static bool ApplyExpressionToXmlNode(XmlDocument xdoc, string nodePath, string expression) | |
{ | |
bool anyHits = false; | |
foreach (var n in xdoc.SelectNodes(nodePath)) | |
{ | |
n.InnerText = expression; | |
anyHits = true; | |
} | |
return anyHits; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Registers a registration for a tile and one for a toast. Toast gets all messages tagged with 'urgent' or 'alert', Tile gets all messages tagged with 'urgent' or 'all'. Tile is a peek tile with a small and a big image URL. Both images live either locally in the app as a resource or can be externally hosted (e.g. also on CDN) or even dynamically generated. Both notifications deliver a headline and a message content line.
Input message key/value schema:
Available tags: all, urgent, alert