Sending a Tile Notification

,

Sending a tile notification is done in the same manner as sending a toast notification; the differences are in the request headers and the format of the XML message (see Listing 15.2). As with toast notifications, we create an HttpWebRequest, build the message, convert the message to a byte array, and then write it to the request Stream. Finally, we verify that the notification was correctly received by the MPNS.

An HttpRequestHeader named WindowsPhone-Target with the value tile is added to the request’s Headers collection, indicating to the MPNS that it is a tile notification.

LISTING 15.2. Sending a Tile Notification


public PushNotificationSendResult SendTileNotification(string title, int count,
    string imagePath, string url, PushNotificationPriority priority)
{
    string priorityHeader;
    if (priority == PushNotificationPriority.RealTime)
    {
        priorityHeader = "1";
    }
    else if (priority == PushNotificationPriority.Priority)
    {
        priorityHeader = "11";
    }
    else
    {
        priorityHeader = "21";
    }

    /* Create the http message that will be sent
     * to the Microsoft hosted server. */
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "POST";
    request.ContentType = "text/xml";
    request.Headers = new WebHeaderCollection
                {
                    { "X-WindowsPhone-Target", "token" },
                    { "X-NotificationClass", priorityHeader }
                };

    /* The XML envelope contains the notification text. */
    string messageFormat = "<?xml version='1.0' encoding='utf-8'?>" +
                        "<wp:Notification xmlns:wp='WPNotification'>" +
                            "<wp:Tile>" +
                                "<wp:BackgroundImage>{0}</wp:BackgroundImage>" +
                                "<wp:Count>{1}</wp:Count>" +
                                "<wp:Title>{2}</wp:Title>" +
                            "</wp:Tile>" +
                        "</wp:Notification>";
    /* Insert the message string. */
    string messageFormatted = string.Format(
        messageFormat, imagePath, count, title);
    /* The message is sent as a byte array. */
    byte[] messageBytes = Encoding.Default.GetBytes(messageFormatted);

    request.ContentLength = messageBytes.Length;

    using (Stream requestStream = request.GetRequestStream())
    {
        requestStream.Write(messageBytes, 0, messageBytes.Length);
    }

    /* Get the response after the message is sent. */
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    string notificationStatus = response.Headers["X-NotificationStatus"];
    string subscriptionStatus = response.Headers["X-SubscriptionStatus"];
    string deviceConnectionStatus
                = response.Headers["X-DeviceConnectionStatus"];

    PushNotificationSendResult result = new PushNotificationSendResult(
        PushNotificationEnumConverter.ToNotificationStatus(notificationStatus),
        PushNotificationEnumConverter.ToDeviceConnectionStatus(
                                                       deviceConnectionStatus),
        PushNotificationEnumConverter.ToSubscriptionStatus(subscriptionStatus)
        );
    Debug.WriteLine(result);
    return result;
}


The format for a tile notification is shown in the following fragment:

<?xml version="1.0" encoding="utf-8"?>
<wp:Notification xmlns:wp="WPNotification">
  <wp:Tile>
    <wp:BackgroundImage>
            ProjectSubdirectory/ExampleImage.png
    </wp:BackgroundImage>
    <wp:Count>
            An integer n, with 0 <= n <= 99
    </wp:Count>
    <wp:Title>Example Title</wp:Title>
  </wp:Tile>
</wp:Notification>

Additional properties can be appended within the wp:Tile element to update properties of a flip, cycle, and iconic tile.

When the notification is displayed on the phone, the content of the wp:BackgroundImage element specifies the URL of the tile’s background image. This value can either be a local path within the project, or a remote URI such as www.example.com/ImageName.png.


Caution

A tile’s background image size must not exceed 80KB. In addition, the download time for an image must not exceed 1 minute. If the image size is too large, or the download takes too long, the default image for the tile is used.


The wp:Title element denotes the title text to be displayed at the bottom of the tile.


Note

For information on how to create shell tiles or modify tile properties directly from your app, see Chapter 32, “Conducting Background Activities with Scheduled Actions.”



Tip

Presenting too much information can slow down comprehension of a tile. Therefore, keep the style simple to improve clarity. When designing a background image for a tile, it is important to make different states easily recognizable.

Do not presume that a background image will be placed on a particular screen color. Themes allow the phone’s Start Experience color scheme to be modified. Therefore, when using PNG images as the tile image format, avoid using transparency if possible, as anti-aliasing may produce unintended discoloration around shape boundaries.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.186.234