Skip to content

Instantly share code, notes, and snippets.

@clemensv
Last active December 11, 2015 20:28
Show Gist options
  • Save clemensv/4655204 to your computer and use it in GitHub Desktop.
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.
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;
}
@clemensv
Copy link
Author

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:

  • 'headline' - headline
  • 'message' - body content
  • 'bigimg' - local or public image URL
  • 'smlimg' - local or public image URL

Available tags: all, urgent, alert

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment