Notification Payloads

0
181

Tile notifications include three items of data:

  • A background PNG or JPG image for the tile that should be 173 pixels by 173 pixels in size
  • A text label that overlays the background image
  • A count value that also overlays the background image; for example, the number of new surveys available

A toast notification includes two strings that appear in the toast: the first string is a header, and the second string is optional body text.

You must make sure that the strings you send on both tile and toast notifications fit in the available space.

The following code example from the TileNotificationPayload Builder class in the TailSpin.Web.Survey.Shared project shows how the Surveys service constructs a tile notification message.

C#
public static byte[] Create(string title,
string backgroundImage = null, int count = 0)
{
using (var stream = new MemoryStream())
{
var settings = new XmlWriterSettings
{ Indent = true, Encoding = Encoding.UTF8 };
using (var writer = XmlWriter.Create(stream, settings))
{
if (writer != null)
{
writer.WriteStartDocument();
writer.WriteStartElement(“wp”, “Notification”,
“WPNotification”);
writer.WriteStartElement(“wp”, “Tile”, “WPNotification”);
writer.WriteStartElement(“wp”, “BackgroundImage”,
“WPNotification”);
writer.WriteValue(backgroundImage ?? string.Empty);
writer.WriteEndElement();
writer.WriteStartElement(“wp”, “Count”,
“WPNotification”);
writer.WriteValue(count == 0 ? string.Empty :
count.ToString(CultureInfo.InvariantCulture));
writer.WriteEndElement();
writer.WriteStartElement(“wp”, “Title”,
“WPNotification”);
writer.WriteValue(title);
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
}
byte[] payload = stream.ToArray();
return payload;
}
}
}

The backgroundImage parameter is the URL of the image, which must be no more than 80 KB in size.

The following code example from the ToastNotificationPayload Builder class in the TailSpin.Workers.Notifications project shows how the Surveys service constructs a toast notification message.

C#
public static byte[] Create(string text1, string text2 = null)
{
using (var stream = new MemoryStream())
{
var settings = new XmlWriterSettings { Indent = true,
Encoding = Encoding.UTF8 };
XmlWriter writer = XmlWriter.Create(stream, settings);
writer.WriteStartDocument();
writer.WriteStartElement(“wp”, “Notification”,
“WPNotification”);
writer.WriteStartElement(“wp”, “Toast”, “WPNotification”);
writer.WriteStartElement(“wp”, “Text1”, “WPNotification”);
writer.WriteValue(text1);
writer.WriteEndElement();
writer.WriteStartElement(“wp”, “Text2”, “WPNotification”);
writer.WriteValue(text2);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
byte[] payload = stream.ToArray();
return payload;
}
}