Live Tiles on a Schedule

If you’ve looked at Windows Phone 7 development, you’ll almost certainly have come across push notifications, a mechanism for efficiently delivering messages to a device whether or not your application is currently running. The architecture looks like this.

 

PushNoificationArchitecture

Items in green are “your” bits and items in red are “Microsoft bits”. In other words, we host a cloud service that knows (once the application has been registered) how to get a message to a specific app running on a specific device. On the phone, a shared “push client” running in the background manages all push notification requests.

The “address” of a specific app on a specific device is mapped to a URL which your application (running on the phone) is handed when it registers for push notifications. To send a message to the device, your cloud application makes an HTTP POST to that URL with a specially formatted message (XML payload) that can invoke one of three types of notification.

  • Tile notification – if the user has promoted your app to their homescreen, you can update the background image, title and count
  • Toast notification – an alert-type message that is displayed at the top of the screen (similar to the text message alert). If the toast notification is tapped, the relevant app will start. It’s possible to set the title and sub-title of the alert.
  • RAW notification – a message of up to 1KB that is delivered to your application if it’s running and is discarded otherwise.

It’s a very neat system that works very well. It helps maximise battery life on the device but at the same time ensures the user can remain connected to your application even when it isn’t running. If you want to know more I recommend looking at the excellent sample that comes with the training kit on C9.

What’s less well-known, is that you can also enjoy some of the “Live Tile” goodness without having to worry about any of the intricacies of push notifications. You can set your application’s tile to update on a schedule. This way, the user gets up-to-date information on their homescreen without the need for you to do very much at all.

Say, for example, your application relates to weather forecasting. You could deliver an up-to-date outlook to the users homescreen (assuming you can find a way to represent that effectively in a 128x128 pixel image) every few hours.

The principle is very simple. You set a URL for the location of the image you want to be used as the background image for the tile when the schedule kicks in. There is only one URL – you change the image on the server as appropriate and this gets picked up by the client when it updates.

For the weather example, I may have a set of URLs representing different locations and I simply update the images at those endpoints when the outlook changes. As clients update on the schedule, their homescreen tiles get updated with the new outlook.

The code to do this couldn’t be simpler.

 private ShellTileSchedule shellTileSchedule;

private void CreateShellTileSchedule()
{
  shellTileSchedule = new ShellTileSchedule()
  {
    Recurrence = UpdateRecurrence.Interval,
    Interval = UpdateInterval.EveryHour,
    RemoteImageUri =
    new Uri(@"https://mikeo.co.uk/demo/PushNotificationImages/tile.png")
  };
  shellTileSchedule.Start();
}

You create a ShellTileSchedule object, set various properties and start the schedule. The ShellTileShedule object has the following properties:

  • Interval – the frequency with which the tile should be updated (an enum that can be set to EveryHour, EveryDay, EveryWeek, EveryMonth)
  • MaxUpdateCount – the number of times the schedule will run before it “expires”
  • Recurrence – whether the schedule should run once or repeat (an enum that can be set to Onetime or Interval)
  • RemoteImageUri – this is where the new tile image will be requested from
  • StartTime – allows you to define the start time of the schedule (eg to defer it)

If you add the above code to your Application class and invoke CreateShellTileSchedule() from the constructor you should be all set.

Feel free to try it with my RemoteImageUri: https://mikeo.co.uk/demo/PushNotificationImages/tile.png

Note, if you follow that link you might think it’s not working but it’s actually some white text on a transparent background Smile. The image doesn’t change very often (I’ll try and get something setup to modify the image regularly). But your app should start with its default tile (remember you need to promote it to the homescreen) and update some time later to a new tile background from my URL (right now it’s one of “1”, “2” or “3”).

Note that it can take around an hour for the first update to the tile to take place – there’s nothing you can do to speed it up so debugging is a waiting game…