Sending a Toast Notification

,

Toast notifications require an XML envelope for the notification information. Listing 15.1 demonstrates how the creation of an XML envelope is constructed and sent to the MPNS. An HttpWebRequest is created using the push notification subscription URL. The notification XML is converted to a byte array, and then it is written to the outgoing request Stream. After sending the request, the response is examined to ensure that it was correctly received by the MPNS.


Note

For more information on the X-NotificationClass request header, and setting a notification’s priority level, see the upcoming section “Notification Classes.”


LISTING 15.1. PushNotifier Class (excerpt)


public PushNotificationSendResult SendToastNotification(
    string title, string message, string url, PushNotificationPriority priority)
{
    string priorityHeader;
    if (priority == PushNotificationPriority.RealTime)
    {
        priorityHeader = "2";
    }
    else if (priority == PushNotificationPriority.Priority)
    {
        priorityHeader = "12";
    }
    else
    {
        priorityHeader = "22";
    }

    /* 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", "toast" },
                    { "X-NotificationClass", priorityHeader },
                    { "X-MessageID", Guid.NewGuid().ToString() }
                };

    /* The XML envelope contains the notification text. */
    string messageFormat = "<?xml version='1.0' encoding='utf-8'?>" +
                        "<wp:Notification xmlns:wp='WPNotification'>" +
                            "<wp:Toast>" +
                                "<wp:Text1>{0}</wp:Text1>" +
                                "<wp:Text2>{1}</wp:Text2>" +
                            "</wp:Toast>" +
                        "</wp:Notification>";
    /* Insert the message string. */
    string messageFormatted = string.Format(messageFormat, title, message);
    /* The message will be 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 deviceConnectionStatus
               = response.Headers["X-DeviceConnectionStatus"];
    string subscriptionStatus = response.Headers["X-SubscriptionStatus"];

    PushNotificationSendResult result = new PushNotificationSendResult(
        notificationStatus, deviceConnectionStatus, subscriptionStatus);

    Debug.WriteLine(result);
    return result;
}


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

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

<?xml version='1.0' encoding='utf-8'?>
<wp:Notification xmlns:wp='WPNotification'>
  <wp:Toast>
    <wp:Text1>WP Unleashed</wp:Text1>
    <wp:Text2>Toast example</wp:Text2> " +
  </wp:Toast>
</wp:Notification>

When the notification is displayed on the phone, the content of the wp:Text1 element is placed in the title field of the notification overlay, while the content of the wp:Text2 element is placed in the body field (see Figure 15.5).

Image

FIGURE 15.5 Anatomy of a toast notification overlay

By default, the app icon is the ApplicationIcon.png file present in the root directory of the Windows Phone project. This can be customized from within the project’s properties editor (see Figure 15.6).

Image

FIGURE 15.6 The application icon image can be set via the Application tab on the properties editor, within Visual Studio.

Alternatively, the path to the image for the application icon can be modified manually within the WMAppManifest.xml file at the Deployment/App/IconPath element.

<IconPath IsRelative="true"
          IsResource="false">ApplicationIcon.png</IconPath>


Tip

As you can see in Figure 15.5, it is wise to tailor the application icon so that it displays clearly at a small size when presented in a toast notification.


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

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