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.
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 . 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…
Comments
Anonymous
November 23, 2010
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, ChrisNTRAnonymous
November 23, 2010
The comment has been removedAnonymous
November 23, 2010
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 :). MikeAnonymous
November 23, 2010
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. MikeAnonymous
November 23, 2010
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.Anonymous
November 23, 2010
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. :) MikeAnonymous
November 24, 2010
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?Anonymous
November 24, 2010
Hi Nigel No, sorry, the local application doesn't have any way to reference the tile in the current release. MikeAnonymous
November 25, 2010
The comment has been removedAnonymous
November 25, 2010
The comment has been removedAnonymous
November 25, 2010
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.Anonymous
November 25, 2010
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 MikeAnonymous
November 25, 2010
The comment has been removedAnonymous
November 29, 2010
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.aspxAnonymous
November 29, 2010
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. MikeAnonymous
November 29, 2010
Thanks Mike, That will work for now. Best Regards, Mark MonsterAnonymous
December 07, 2010
I think it was a good decision to restrict the platform in the first version so developers get used to it it in The Right Way (tm). You can still open up later for special purposes. But, why isn't it possible to update the tile on a schedule with code? E.g. it could be possible to have a function in your code that would be called every X-Min and is only allowed to run e.g. 1min...Anonymous
December 07, 2010
Hi HadesMR That wouldn't work as your app has to be in the foreground to be running. And if your app is in the foreground then, by definition, the live tile isn't visible. I do think there's merit in being able to request an update to the current tile and to be able to check whether a schedule is enabled from your code. MikeAnonymous
December 23, 2010
Hi mike and thanks for this post. A couple of questions:
- Does setting max update count to 0 result in the schedule running for ever? 2.if I set the recurrence to interval and the max update count to 10 will that repeat for ever?