Category: Windows Phone SDK 7.1.1 Update

EvenTiles and Push Notifications

In the previous part of EvenTiles, you saw that applications running on 256-MB Devices cannot use Periodic Agents. Of course the application is still working, but the content of the backside of a Secondary Tile, when pinned to the start screen, does not alter. Since 256-MB Windows Phone Devices fully support Push Notifications, it is possible to change the Secondary Tile content with a little extra effort. In this episode of EvenTiles we will take a look at adding Tile Notifications to the application. Those Tile Notifications are delivered to individual Windows Phone devices through the Microsoft Push Notification Service, just like all other Push Notifications.

In this blog entry we will concentrate on the client side, mainly providing code that registers the application. To test the application, we will make use of a little ASP.NET application that is based on the sample that is introduced in the How to Send and Receive Tile Notifications for Windows Phone article. It is beyond the scope of this blog entry to show how to host a web service (for instance by making use of Azure functionality).

NOTE: The ASP.NET test application cannot be created using Visual Studio 2010 Express for Windows Phone. You either need at least Visual Studio 2010 Professional or the free Visual Web Developer 2010 Express to create the test application.

Push Notifications on Secondary Tiles

Something that is not entirely clear in the referred How to article, or in the documentation around Push Notifications on MSDN is the fact that sending Tile Updates through Push Notifications is not limited to the Application Tile, but you can also use the same approach to update Secondary Tiles. Since we are dealing with a Secondary Tile in the EvenTiles application, we will focus on updating a Secondary Tile. In the ASP.NET test application we will create the following message to update our tile:

Updating a Seondary Tile
  1. // Create the Tile message.
  2. string tileMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
  3. "<wp:Notification xmlns:wp=\"WPNotification\">" +
  4.     "<wp:Tile ID=\"/MainPage.xaml?TileId=Secondary\">" +
  5.       "<wp:BackContent>" + TextBoxBackContent.Text + "</wp:BackContent>" +
  6.       "<wp:Count>" + nrRuns.ToString() + "</wp:Count>" +
  7.    "</wp:Tile> " +
  8. "</wp:Notification>";

By explicitly specifying the ID of a tile you can inform the Push Notification Service that you want to update a specific (Secondary) Tile. Omitting the ID passes the update to the Application Tile. Since EvenTiles creates the Secondary Tile as shown below, passing the Tile Notification message as shown above will update the Secondary Tile if it is pinned to the Start Screen. In the sample we only update the BackContent of the Secondary Tile and the count on the front of it, but it is possible to update more properties of the Secondary Tile. Of course it is important to use the same URI for the Secondary Live Tile when updating it through a Tile Notification.

Creating a Secondary Tile
  1. private void btnInstall_Click(object sender, RoutedEventArgs e)
  2. {
  3.     StandardTileData NewTileData = new StandardTileData
  4.     {
  5.         BackgroundImage = new Uri("Background.png", UriKind.Relative),
  6.         Title = "EvenTiles",
  7.         Count = 0,
  8.         BackBackgroundImage = new Uri("BackBackTile.png", UriKind.Relative),
  9.         BackTitle = "EvenTiles",
  10.         BackContent = App.ActualSecBackContent
  11.     };
  12.     ShellTile.Create(new Uri("/MainPage.xaml?TileId=Secondary", UriKind.Relative), NewTileData);
  13. }

In order to receive Tile Notifications in EvenTiles, the application needs to create a HttpNotificationChannel or attach to an already created channel if the application is started again on a particular Windows Phone device. The following code snippet shows how to setup a new notification channel. If the channel is successfully opened, it will pass a URI through the ChannelUriUpdated event handler. This URI is used to send notifications to the particular Windows Phone Device that received this URI from the Push Notification Service. This URI is unique for the application and the phone on which the application is running. If the channel already exists, this method simply starts listening for channel changes or errors through the ChannelUriUpdated and ErrorOccurred events respectively.

Getting a Notification Channel
  1. public class TileNotifications
  2. {
  3.     public const string tileNotificationChannel = "EvenTilesChannel";
  4.  
  5.     public void SetupChannel()
  6.     {
  7.         var pushChannel = HttpNotificationChannel.Find(tileNotificationChannel);
  8.         if (pushChannel == null)
  9.         {
  10.             pushChannel = new HttpNotificationChannel(tileNotificationChannel);
  11.             pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
  12.             pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
  13.             pushChannel.Open();
  14.             pushChannel.BindToShellTile();
  15.         }
  16.         else
  17.         {
  18.             pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
  19.             pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
  20.  
  21.             // Display the URI for testing purposes. Normally, the URI would be passed back to a web service.
  22.             System.Diagnostics.Debug.WriteLine(pushChannel.ChannelUri.ToString());
  23.         }
  24.     }

If you want to send out push notifications from a web service, the Windows Phone application typically would send this URI to a web service, that in turn uses it to deliver the push notifications to the registered device. In our test application we simply display the URI in the debug windows of Visual Studio to be able to copy it to our ASP.NET test application.

If you are no longer interested in receiving Tile Notifications for your application, you can close a previously opened HttpNotificationChannel, make sure to dispose of it and also make sure to unsubscribe to events to prevent against leaking memory. The following code snippet shows how you can stop receiving Tile Notifications:

Closing a Notification Channel
  1. public void CloseChannel()
  2. {
  3.     var pushChannel = HttpNotificationChannel.Find(tileNotificationChannel);
  4.     if (pushChannel != null)
  5.     {
  6.         pushChannel.ChannelUriUpdated -= PushChannel_ChannelUriUpdated;
  7.         pushChannel.ErrorOccurred -= PushChannel_ErrorOccurred;
  8.         pushChannel.Close();
  9.         pushChannel.Dispose();
  10.         App.GetLiveTileNotifications = false;
  11.     }
  12. }

Finally, in our EvenTiles application we implement some simplified event handling to react on push channel changes and errors. This implementation is for demonstration purposes only and can be used to manually pass a channel URI to an ASP.NET application or to close down / reinitialize the push channel on certain errors. The following code snippet shows how we simply write the channel URI to the Output Window of Visual Studio.

Handling Push Channel Events
  1. void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
  2. {
  3.     switch (e.ErrorType)
  4.     {
  5.         case ChannelErrorType.ChannelOpenFailed:
  6.             App.GetLiveTileNotifications = false;
  7.             break;
  8.         case ChannelErrorType.PayloadFormatError:
  9.             SetupChannel();
  10.             break;
  11.         default:
  12.             CloseChannel();
  13.             break;
  14.     }
  15. }
  16.  
  17. void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
  18. {
  19.     System.Diagnostics.Debug.WriteLine(e.ChannelUri.ToString());
  20.     App.GetLiveTileNotifications = true;
  21. }

imageTo send Tile Notifications to EvenTiles, make sure to run EvenTiles and enable Tile Notifications through the Settings Page. Once Tile Notifications are enabled, you can copy the channel URI from the Visual Studio Output Window and paste it in the ASP.NET test application. After this step, you can send tile updates to EvenTiles.

 

imageThe ASP.NET test application does so in a ThreadPool thread. You can specify the number of updates and the time between updates. Each time another update is send, the count property that will be displayed on the front side of the Secondary Tile that EvenTiles pinned on the start screen will be updated.

Even though Background Agents are not supported on 256-MB devices, we have a nice workaround by making use of Push Notifications because they are fully supported. Of course this means that you need to invest more in server side programming, but it also means that the end user experience of a Windows Phone application can be very similar on both a 512-MB device and a 256-MB device. For this to work, the target device must have a network connection.

The complete sample code for EvenTiles so far can be downloaded here. If you want to test the application on a 256-MB device (emulator), you will need to download and install the Windows Phone 7.1.1 SDK Update as well. The ASP.NET test application is also available as a separate download. In order to run this test application, you need a version of Visual Studio 2010 that supports creating  and debugging ASP.NET applications.

The following video shows how to subscribe to Tile Notifications from within the EvenTiles application and how to test the newly added functionality by means of an APS.NET test application.

Using Push Notifications to update Live Tiles on Windows Phone

EvenTiles and 256-MB Windows Phone Devices

In part 18 of the continuing story of EvenTiles we will take a look at targeting additional Windows Phone devices that will shortly hit the market in several countries. A few weeks ago, the Windows Phone 7.1.1 SDK Update became available for download. This SDK contains everything you need to develop Windows Phone applications that target the new 256-MB devices. Compared to Windows Phone 7.5 devices, these devices have less memory available, but they can run almost all applications that will run on Windows Phone 7.5 devices as long as they don’t use excessive amounts of memory and as long as they don’t make use of generic background services.

Even though the latter is no problem for most applications (especially since users might have disabled background processing for a particular application on Windows Phone 7.5 devices), in the case of EvenTiles this is kind of an interesting limitation. After all, EvenTiles was originally created to show you how to develop Windows Phone applications, but background processing became one of the more important features of the application. Having said that, EvenTiles will definitely run on 256-MB Windows Phone devices. The only exception is the fact that the background of the Secondary Tile that can be pinned to the Start Screen will only contain one single string.

Let’s just take a look at how to detect if a Windows Phone application is running on a 256-MB device and how we can limit functionality in that particular case. Since background processing is unavailable on a image256-MB device, it makes a lot of sense to remove the functionality to start and re-schedule a PeriodicTask when EvenTiles runs on such a device. The code to start / stop a PeriodicTask in EvenTiles can be found in the MainPage class in two separate methods (StartPeriodicAgent and RemovePeriodicAgent). This code is also used in the MainPage constructor to optionally renew the PeriodicTask when the Even Tiles application is started.

After installing the Windows Phone 7.1.1 SDK Update, you will not only get a new emulator that allows you to test your application on a 256-MB target, but you will also have the ability to check the amount of memory that your target device can use to distinguish between 256-MB and 512-MB Windows Phone devices. To do so, you will have to inspect the value of a property inside the DeviceExtendedProperties. This value can be retrieved through the ApplicationWorkingSetLimit property, which returns a long integer. The interesting thing about this property is that it may or may not exist on particular Windows Phone devices. If the property exists, it will give back a value representing the current application’s memory working set limit on a device. If this value contains the equivalent of 90 MB, the application is running on a 256-MB Windows Phone device, otherwise it is running on a 512-MB device. However, if the application is running on a device that does not contain the latest Windows Phone operating system update, the ApplicationWorkingSetLimit property will not be defined and an exception will be thrown when trying to retrieve the value of that property. In this case, the exception can be treated as ‘expected’ behavior for 512-MB devices. This means we can detect the device we are running on by making use of the following code snippet:

Running on a 256-MB Device?
  1. public const string keyIsLowMemDevice = "K_256MB";
  2. public static bool IsLowMemDevice { get; private set; }
  3.  
  4. private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
  5.  
  6. // Code to execute when the application is launching (eg, from Start)
  7. // This code will not execute when the application is reactivated
  8. private void Application_Launching(object sender, LaunchingEventArgs e)
  9. {
  10.     if (!appSettings.Contains(keyIsLowMemDevice))
  11.     {
  12.         try
  13.         {
  14.             Int64 result = (Int64)DeviceExtendedProperties.GetValue("ApplicationWorkingSetLimit");
  15.             if (result < (long)(90 * 1024 * 1024))
  16.                 IsLowMemDevice = true;
  17.             else
  18.                 IsLowMemDevice = false;
  19.         }
  20.         catch (ArgumentOutOfRangeException)
  21.         {
  22.             // Windows Phone OS update not installed, which indicates a 512-MB device.
  23.             IsLowMemDevice = false;
  24.         }
  25.         appSettings[keyIsLowMemDevice] = IsLowMemDevice;
  26.     }
  27.     else
  28.     {
  29.         IsLowMemDevice = (bool)appSettings[keyIsLowMemDevice];
  30.     }
  31. }

In this code snippet, we store a boolean value in the application settings to indicate if we are running on a low memory device. To determine the device on which the application is running, we inspect the DeviceExtendedProperties once. The reason to do this only once is to not continuously run into exceptions, which would in fact hurt performance during application initialization on a device that does not have the latest operating system update. Once we know on which device we are running, we can now either enable or disable background processing as is shown in the next code snippet that takes care of pinning / removing a Secondary Tile as a result of clicking the install button:

No Background Agent on 256-MB
  1. private void btnInstall_Click(object sender, RoutedEventArgs e)
  2. {
  3.     if (secondaryTileInstalled)
  4.     {
  5.         var secondaryTile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("TileId=Secondary"));
  6.         if (secondaryTile != null)
  7.         {
  8.             if (!App.IsLowMemDevice)
  9.             {
  10.                 RemovePeriodicAgent();
  11.             }
  12.             secondaryTile.Delete();
  13.             btnInstall.Content = txtInstallTile;
  14.             secondaryTileInstalled = false;
  15.         }
  16.     }
  17.     else
  18.     {
  19.         if (!App.IsLowMemDevice)
  20.         {
  21.             StartPeriodicAgent();
  22.         }
  23.  
  24.         StandardTileData NewTileData = new StandardTileData
  25.         {
  26.             BackgroundImage = new Uri("Background.png", UriKind.Relative),
  27.             Title = "EvenTiles",
  28.             Count = 0,
  29.             BackBackgroundImage = new Uri("BackBackTile.png", UriKind.Relative),
  30.             BackTitle = "EvenTiles",
  31.             BackContent = App.ActualSecBackContent
  32.         };
  33.  
  34.         ShellTile.Create(new Uri("/MainPage.xaml?TileId=Secondary", UriKind.Relative), NewTileData);
  35.     }
  36. }

From a functional point of view, it now makes perfect sense to run EvenTiles on 256-BM devices as well. However, we can take it one step further if you want to have different data on the back side of a Secondary Tile. Since Push Notifications are fully supported on 256-MB Windows Phone devices, you can use those to update the application tile, thus we will extend EvenTiles with Push Notification support.

There are even more optimizations that we can make when EvenTiles is running on a 256-MB device. To transfer data between the application and its PeriodicTask we were using a file, as explained when we discussed how data can be exchanged between different processes. This means that we don’t need that file when running on a 256-MB device. This makes running the application on a 256-MB device even more efficient. In case of a 256-MB device, we only store the string that needs to be displayed in the application settings. On all other devices, we also store that string in a file through the TileData helper class so the PeriodicTask can retrieve it, as shown in the following code snippet.

No transfer on 256-MB device
  1. protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
  2. {
  3.     if (!App.ActualSecBackContent.Equals(tbBackContent.Text))
  4.     {
  5.         App.ActualSecBackContent = tbBackContent.Text;
  6.         if (!App.IsLowMemDevice)
  7.         {
  8.             TileData.SecondaryBackContent = tbBackContent.Text;
  9.             TileData.ShowDefaultSecondaryBackContent = false;
  10.         }
  11.  
  12.         var TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("TileId=Secondary"));
  13.  
  14.         if (TileToFind != null)
  15.         {
  16.             StandardTileData NewTileData = new StandardTileData
  17.             {
  18.                 BackContent = tbBackContent.Text
  19.             };
  20.  
  21.             TileToFind.Update(NewTileData);
  22.         }
  23.     }
  24.     base.OnNavigatedFrom(e);
  25. }

imageIf you are running EvenTiles on either a 256-MB Device or a 512-MB Device you don’t really experience any difference in performance. The application runs well, and it does not start noticeably slower on a 256-MB Device. Of course, EvenTiles uses only a limited amount of memory. The only difference you will notice is that the backside of the Secondary Tile will not display alternating texts. The reason is the absence of Background Agents on 256-MB devices, as you can see in the different screen dumps of both a 256-MB and a 512-MB emulator.

The complete sample code for EvenTiles so far can be downloaded here. If you want to test the application on a 256-MB device (emulator), you will need to download and install the Windows Phone 7.1.1 SDK Update as well. After installing this update, you can even run your application simultaneously on a 512-MB and a 256-MB emulator and compare the behavior on both of them.

MultipleEmulators

The following video shows how to detect if your application is running on a 256-MB device and it shows how to limit functionality in that case.

Limiting functionality when running on 256-MB Windows Phone Devices

In the next episode of EvenTiles we will start talking about Push Notifications.