Mike Ormond's Blog

Musings on mobile development and Windows Phone 7 in particular.

Live Tiles on a Schedule

Live Tiles on a Schedule

  • Comments 19

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(@"http://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: http://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…

  • Nice sample Mike.

    Something else to worth noting is that since this is technically still a Push Notification, you will need to prompt your user to allow push notifications before you set a push notification schedule. Once you have done that, you will also need to allow a user to disable push notifications (live tiles), which would end up leaving a "live tile" on your homepage with no way of returning the user back to the original non-live tile and a horrible user experience. If you don't allow a user to disable the push notification, it would fail the WP7 Application Requirements when submitting your application to the marketplace.

    Since the CreateShellTileSchedule method is going to run every time the application does, how does this effect the push notifications that are sent out by the device, will this mean that it will re-start a schedule or will it ignore the new schedule creation if one already exists. If it does re-start an existing schedule, is there a way for a developer to know if a existing schedule has been set up?

    Can the remote image tile be of any size? The normal tile size is 173x173 where as your live tile is 128x128.

    You're right about there being no way of debugging this works apart from waiting an hour, I would love for Microsoft to improve this debugging experience since it is really hard to work with.

    Thanks,

    ChrisNTR

  • Hi Mike, that's all well and good, but I would like to create an app that updates a Live Tile with device information in real-time, such as battery percentage, remaining storage space or WiFi signal strength.

    Now, I know the answer to my question: "You can't", but I cannot see a good technical reason why developers should be restricted so much.  And don't say battery life, as that's bullshit.

    Regards, Alex Roebuck.

  • Hi Chris

    That's a lot of points / questions :) And as you know it was your question that originally got me looking at this. Anyway, here goes:

    "since this is technically still a Push Notification, you will need to prompt your user to allow push notifications before you set a push notification schedule." - I don't think this is the case as it's a tile rather than a toast. 6.2.1 and 6.2.2 apply specifically to toast notifications. Unless there is another requirement I've missed?

    "how does this effect the push notifications that are sent out by the device" - I don't know but I'll try and find out.

    "Can the remote image tile be of any size" - according to the UI Design and Interaction Guide, tiles are cropped / scaled as necessary though it would have been better if I'd chosen the "right" size. What I actually did was look at the size of the images used in the C9 training kit sample :).

    Mike

  • Hi Alex

    You're right, the answer is you can't. I wouldn't have said battery life was #1 for your scenario - but battery life is a key factor in many other design decisions that were made. And actually, battery life could still be a factor in your case - we restrict the schedule to a max frequency of 1 hr for this reason, you'd need to be updating an awful lot faster than that for your tile to be useful. We implemented a lot of stuff. And a lot of stuff didn't make it into the API. The access you want isn't there. It may be one day, but it's not there today. We've taken a very deliberate "end user experience first" approach - and been very hardline about it. That means some things developers are used to having access to, and some of the freedom they've had to access features isn't there in this release. Sorry about that but I prefer to focus on all the great things you *can* do with WP7. We'll see what the future brings in due course.

    Mike

  • Thanks Mike, for the most comprehensive answer I have had on this subject.

    I understand the motivation, but I still find it a little frustrating that every app idea I have turns out to be impossible using the WP7 API.  A few examples: the aforementioned "status" tiles, an "Application Hub" (impossible as you cannot enumerate the installed apps), a profiles application (impossible as an app cannot run in the background), and so on.

    Alex.

  • Hi Chris

    On the one I didn't know the answer to, I do now :)

    "Since the CreateShellTileSchedule method is going to run every time the application does, how does this effect the push notifications that are sent out by the device, will this mean that it will re-start a schedule or will it ignore the new schedule creation if one already exists. If it does re-start an existing schedule, is there a way for a developer to know if a existing schedule has been set up?"

    There's only ever one instance of the schedule for your app. If one doesn't exist it gets created, If it already exists, it's replaced. There's no way to find out if a schedule already exists.

    And yes, they're looking at ways to make testing easier in future releases. :)

    Mike

  • One thing I'd love to do is be able to update the "Count" from within the application.

    Doesn't even really need to be on a schedule. Will this be possible?

  • Hi Nigel

    No, sorry, the local application doesn't have any way to reference the tile in the current release.

    Mike

  • Is it possible to generate Tiles for individual users? Can each phone have a separate URL referencing the Tile.png?

    like: mikeo.co.uk/.../tile.png

  • Hi Stephan

    I see no reason why not. Whether you could use a querystring parameter or not, I don't know. You could always modify the URL in some other way. eg

    http_://mikeo.co.uk/demo/PushNotificationImages/<InsertSomePhoneIDHere>/tile.png

    Mike

  • As a smartphone developer, the whole idea that I have to host a web service so my smartphone app can update its icon is preposterous.  

    Presumably the inbuilt Calendar, Messaging, E-Mail and Hotmail apps do not use push notifications or tile schedules to update their tiles?  This implies that "third party" developers are deliberately being prevented from using this capability.  

    The Android AppWidgetManager and RemoteViews object is a much more flexible design.

    My apps are mostly utilities and tools.  I really want to develop for Windows Phone 7, but it seems at the moment that it is only good for games, media and web content apps.

  • Hi Alex?

    It's not preposterous, it's a consequence of the fact your app isn't running when it's in the background. That's a design decision we took with end-users in mind.

    Best regards

    Mike

  • Hi Mike,

    I understand, but I think the decision is bad for end-users.  

    On my Android phone, I can see if I have received any Tweets, if my battery will soon need charging, if I have received any facebook messages, even if there are delays on the Jubilee Line, at a glance.  I can run a profiles app which will automatically turn down the phone volume when I am in a meeting, or turn WiFi off when I am away from home.

    On my Windows Phone 7, I have to start the Twitter app (and wait for it to refresh) before I can see if I have received any new tweets.  Same with facebook. If I am out of reception, those apps don't even work.

    As a WP7 developer, I am not able to write an app that does any of the following: A replacement SMS messenger (like Handcent SMS), a phone profiles app (like Locale or Tasker), or an app launcher (like ADW.Launcher).  I cannot write an alarm app, a file explorer, a backup app, a WiFi signal analyser, a signal strength indicator, or battery life tile.

    I don't want to make this an Android versus WP7 rant, as Android has problems of its own and WP7 is slick, refreshing and new, but from a developer's point-of-view, I am finding it very frustrating.  Visual Studio 2010, the emulator, and the WP7 designer tools are superb, but I just cannot write any of the apps that I want to.

    This week, Microsoft are shouting about the fact that 15,000 developers have signed up for WP7.  I am one of them.  However, I think a lot of those developers are going to be very disappointed when they discover what they can, or more importantly, cannot do with WP7.

    Kind Regards, Alex.

  • Hi Mike,

    I read from your comments "There's no way to find out if a schedule already exists.".

    To me that's a little bit strange. Let's say I want to have an on-off switch in my settings page of the app.

    Let's determine the scenario:

    1:It will start with "off".

    2: I turn it on, and store the setting in Isolated Storage (needs to be stored because I can't retrieve it from somewhere else)

    3: Schedule is running, and tile get's updated.

    4: I move to a environment where the connection isn't reliable.

    5: Tile update fails (download of tile takes more than 1 minute).

    6: Tile update fails again (download of tile takes more than 1 minute).

    7: Tile update fails again (download of tile takes more than 1 minute).

    8: Tile schedule gets disabled.

    9 Tile won't be updated anymore, but there's no way to find out in the app itself.

    Is there a solution that I'm not aware of yet?

    Best Regards,

    Mark Monster

    PS: Also a question from my hand in the Silverlight forums: forums.silverlight.net/.../211000.aspx

  • Hi Mark

    I think the only option is to check on application start whether the Live Tile is enabled in settings and recreate the schedule if it is. Not ideal, particularly for longer period schedules, but I think it's the best you can achieve under the circumstances.

    Mike

Page 1 of 2 (19 items) 12