In a follow up to yesterday’s article about Tile Notifications, let’s take a look at sending Toast Notifications.
You’ll remember that we had two projects yesterday, SendTileServer and TilesNotificationClient. Today we’ll create SendToastServer and ToastNotificationsClient. The parallels are obvious.
To get started, let’s modify SendTileServer to handle sending Toast notifications. Create a new blank ASP.NET application just as you did yesterday. If you like, to save coding, you can simply update the keyword SendTile to SendToast and tileMessage to toastMessage. With that done, the only significant changes are when you create the message:
string toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<wp:Notification xmlns:wp=\"WPNotification\">" + "<wp:Text1>" + TextBoxTitle.Text.ToString() + "</wp:Text1>" + "<wp:Text2>" + TextBoxSubTitle.Text.ToString() + "</wp:Text2>" + "</wp:Toast> " + "</wp:Notification>";
and when you set the Notification headers:
sendNotificationRequest.Headers.Add( "X-WindowsPhone-Target", "toast"); sendNotificationRequest.Headers.Add( "X-NotificationClass", "2");
The Toast Notification Client
The client changes even less than the server. Make a copy of yesterday’s program and change the word Tile to Toast wherever it appears. Key is changing the pushchannel,
pushChannel.BindToShellToast();
The default is for the toast not to display when your application is running, if you wish to receive notifications while your application is running, add the following,
pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>( PushChannel_ShellToastNotificationReceived);
Next, if you want to receive notification when your application is running, implement the event handler,
void PushChannel_ShellToastNotificationReceived( object sender, NotificationEventArgs e) { StringBuilder message = new StringBuilder(); string relativeUri = string.Empty; message.AppendFormat("Received Toast {0}:\n", DateTime.Now.ToShortTimeString()); foreach (string key in e.Collection.Keys) { message.AppendFormat("{0}: {1}\n", key, e.Collection[key]); if (string.Compare( key, "wp:Param", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.CompareOptions.IgnoreCase) == 0) { relativeUri = e.Collection[key]; } } Dispatcher.BeginInvoke(() => MessageBox.Show(message.ToString())); }
For the complete source code and write up of this example, please be sure to see the original posting from which this and the previous were shamelessly stolen.