IT tutorials
 
Technology
 

Windows Phone 8 : Using Push Notifications (part 4) - Sending Toast Notifications

9/21/2013 2:07:41 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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");
    });
}


Toast Notifications

If a toast message comes in while the application is running, the toast notification will not appear. Toast notifications appear only when your application is not currently in the foreground.


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.

 
Others
 
- Windows Phone 8 : Using Push Notifications (part 3) - Raw Notifications
- Windows Phone 8 : Using Push Notifications (part 2) - Setting Up the Server for Push Notifications
- Windows Phone 8 : Using Push Notifications (part 1) - Preparing the Application for Push Notifications
- InfoPath with SharePoint 2010 : Document Information Panel Content Type - Modify the DIP
- InfoPath with SharePoint 2010 : Document Information Panel Content Type - Create the Document Library, Add Columns to Your DIP
- Windows Small Business Server 2011 : Working with Users (part 4) - Creating User Roles
- Windows Small Business Server 2011 : Working with Users (part 3) - Managing User Properties
- Windows Small Business Server 2011 : Working with Users (part 2) - Creating Multiple User Accounts
- Windows Small Business Server 2011 : Working with Users (part 1) - Creating a User Account
- Active Directory 2008 : Managing Operations Masters (part 3) - Seizing Operations Master Roles, Returning a Role to Its Original Holder
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us