IT tutorials
 
Technology
 

Windows Phone 8 : Using Push Notifications (part 5) - Creating Live Tiles

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

6. Creating Live Tiles

Sending a Live Tile notification is similar to sending a raw notification, but first you must bind your channel to the shell via the BindToShellTime (after first checking to ensure that the channel hasn’t already been bound):

// Bind to the Shell Tile
if (!_myChannel.IsShellTileBound)
{
  _myChannel.BindToShellTile();
}

Binding it to the shell in this way tells the phone to expect Live Tile notifications. Over on the server side, the code is similar to raw push notifications, but a couple of small changes need to occur. The body of the Live Tile update must be in the format of a specific XML document:

<?xml version=""1.0"" encoding=""utf-8""?>
<wp:Notification xmlns:wp=""WPNotification"">
  <wp:Tile>
    <wp:BackgroundImage>URL</wp:BackgroundImage>
    <wp:Count>0</wp:Count>
    <wp:Title>Title</wp:Title>
    <wp:BackBackgroundImage>URL</wp:BackBackgroundImage>
    <wp:BackTitle>Title</wp:BackTitle>
    <wp:BackContent>Content</wp:BackContent>
  </wp:Tile>
</wp:Notification>

The format of this file is similar to the different tile types indicated in WMApplication.xml file. The .xml file represents the data for the specific type of tile.

After you create this XML document, you are ready to push the change to the phone. So when you push this update from the server, you need to make a couple of small changes from the raw push notifications:

string SendTileMessage(string pushUri, string imageUrl)
{
  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 Tile Immediate requires class == 1
  request.Headers.Add("X-NotificationClass", "1");

  // Specify that the message is a Toast message
  request.Headers.Add("X-WindowsPhone-Target", "token");

  // Create the XML of the Tile Message
  string tileMessage = @"<?xml version=""1.0"" encoding=""utf-8""?>
  <wp:Notification xmlns:wp=""WPNotification"">
    <wp:Tile>
      <wp:BackgroundImage>{0}</wp:BackgroundImage>
      <wp:Count>0</wp:Count>
      <wp:Title>Fun With Push</wp:Title>
      <wp:BackTitle>Back of Tile</wp:BackTitle>
      <wp:BackContent>More Content</wp:BackContent>
    </wp:Tile>
  </wp:Notification>";

  string tileXml = string.Format(tileMessage, imageUrl);

  // Send it
  byte[] notificationMessage = Encoding.UTF8.GetBytes(tileXml);
  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);
  }
}

The first change is to ensure that the ContentType is set to text/xml because the body will be the XML document you’re going to create. Next the X-NotificationClass header needs to be set to 1 for tile updates. Like the raw and toast notifications, you can delay the delivery of the notification by adding 10 or 20, making the valid X-NotificationClass values 1, 11, and 21 for Live Tile updates. Next, you need to add a new header for the X-WindowsPhone-Target header and specify its value as token to tell the MPNS that this is a tile update. Lastly, you simply need to format the XML message as described earlier. Pushing this to the phone will enable you to update the tile on the phone (if it’s pinned to the home screen).

It’s a common practice to send an Internet-accessible URI for the BackgroundImage value. The image must be exactly 173×173 pixels in size and be a 24-bit .png file.

If you need to update a secondary tile, you must include a property of the tile called Id that has the URI used for the secondary tile:

  // Create the XML of the Tile Message
  string tileMessage = @"<?xml version=""1.0"" encoding=""utf-8""?>
  <wp:Notification xmlns:wp=""WPNotification"">
    <wp:Tile Id="/views/AnotherView.xaml?flightNo=465">
      <wp:BackgroundImage>{0}</wp:BackgroundImage>
      <wp:Count>0</wp:Count>
      <wp:Title>Fun With Push</wp:Title>
      <wp:BackTitle>Back of Tile</wp:BackTitle>
      <wp:BackContent>More Content</wp:BackContent>
    </wp:Tile>
  </wp:Notification>";

This Id attribute must match the URI of the secondary tile exactly; otherwise, it will not update the tile and just ignore the request.

 
Others
 
- Windows Phone 8 : Using Push Notifications (part 4) - Sending Toast Notifications
- 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
 
 
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