5. Sending Toast Notifications
Toast notifications are simple messages that
appear on the top of the phone to alert the user with a specific
message from the application. If the user taps the toast message, the
application that notified the user will launch. Toast messages are
shown only when your application is not currently running. They provide
a convenient way to get the user to go to your application and see new
or updated information.
For your application to accept toast messages,
you must tell the phone’s shell service that you want to accept toast
notifications. You do this by adding this code to the phone’s channel
registration:
// Make sure that you can accept toast notifications
if (!_myChannel.IsShellToastBound) _myChannel.BindToShellToast();
Because toast messages are typically received
while the application is not running, they are usually used to alert
users of certain information or to alert them to start the application.
You might also want to be notified when a toast message is sent while your application is running. You can do this by handling the ShellToastNotificationReceived
event on the channel:
_myChannel.ShellToastNotificationReceived +=
channel_ShellToastNotificationReceived;
This event enables you to be alerted if a toast message comes in while the application is running:
void channel_ShellToastNotificationReceived(object sender,
NotificationEventArgs e)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("Toast received while app running");
});
}
Back on the server, creating these types of
messages is similar to creating a raw notification, but the format of
the toast message is specific. The message you send must be a small XML
document that contains information about what to show in the toast
message:
<?xml version="1.0" encoding="utf-8"?>
<wp:Notification xmlns:wp="WPNotification">
<wp:Toast>
<wp:Text1>Title</wp:Text1>
<wp:Text2>Message</wp:Text2>
</wp:Toast>
</wp:Notification>
The message must look exactly like this, except
the first (bolded) part of the message will be in the Text1 section and
the second part (unbolded) will be in the Text2 section. The code to send this message is the same as a raw message, but with some minor changes:
string SendToastMessage(string pushUri, string message)
{
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(pushUri);
// For Raw Update use type of body
// (text/plain or text/xml are typical)
request.ContentType = "text/xml";
// Specify the HTTP verb (must be POST)
request.Method = "POST";
/
// Use a generated unique ID to prevent duplicate push messages
request.Headers.Add("X-MessageID", Guid.NewGuid().ToString());
// Send Toast Immediate requires class == 2
request.Headers.Add("X-NotificationClass", "2");
// Specify that the message is a Toast message
request.Headers.Add("X-WindowsPhone-Target", "toast");
// Create the XML of the Toast Message
string toastMessage = @"<?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>";
string toastXml = string.Format(toastMessage,
"This is Toast",
message);
// Send it
byte[] notificationMessage = Encoding.UTF8.GetBytes(toastXml);
request.ContentLength = notificationMessage.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(notificationMessage,
0,
notificationMessage.Length);
}
// Sends the notification and gets the response.
try
{
HttpWebResponse response =
(HttpWebResponse)request.GetResponse();
return HandleResponse(response);
}
catch (Exception ex)
{
return string.Concat("Exception during sending message",
ex.Message);
}
}
Because the toast message is XML, you must change the content type to text/xml
. The X-NotificationClass
value for toast messages is 2
(instead of 3
for raw messages). Like raw messages, you can add 10 and 20 to increase the time before the message is sent (making 2
, 12
, and 22
the three values for the X-NotificationClass
header). As it is a toast message, you must add a new header called X-WindowsPhone-Target
and set it to toast
to indicate it’s a toast message. Finally, the format of the message is
different, so you need to create the XML message; you can upload it
like any other string.