I am currently working on a feed reader app in HTML5/JavaScript and I have a problem with live tiles. I have a live tile that is updating periodically by querying a web service that is returning the latest 3 articles from a blog. The live tile works as expected, but I want to create a badge notification that will display the unread articles.
The way I have thought to do this is to see if a tile updates and then increment the number from the badge notification. When the user launches the app, the badge will be cleared.
I use the following lines of code:
var notifications = Windows.UI.Notifications;
var polledUri = new Windows.Foundation.Uri("http://my_url/feed.php");
var recurrence = notifications.PeriodicUpdateRecurrence.halfHour;
var tileUpdater = notifications.TileUpdateManager.createTileUpdaterForApplication()
tileUpdater.startPeriodicUpdate(polledUri, recurrence);
This creates the live tile that updates every half an hour. My problem is: I want to create a badge update every time the PeriodicUpdate takes place. I use the following code:
var badgeType = notifications.BadgeTemplateType.badgeNumber;
var badgeXml = notifications.BadgeUpdateManager.getTemplateContent(badgeType);
var badgeAttributes = badgeXml.getElementsByTagName("badge");
var tileUpdater = notifications.TileUpdateManager.createTileUpdaterForApplication()
tileUpdater.startPeriodicUpdate(polledUri, recurrence);
// this is where my problem is
// if (tileUpdater.update() == true) -> This line is not correct: how can I catch the update event?
//badgeAttributes[0].setAttribute("value", currentValue + 1);
var badgeNotification = new notifications.BadgeNotification(badgeXml);
notifications.BadgeUpdateManager.createBadgeUpdaterForApplication().update(badgeNotification);
I want to catch the update event of that tileUpdater.startPeriodicUpdate function and increment the value of the badge. How can I do that? I have searched everywhere and I could not find an answer.
I appreciate your help.
Julian Atanasoae
Even if you could detect that the periodic tile update has occurred (I'm pretty sure you cannot), it will occur most of the time when your app is NOT running. As a result, the code you have to set the badge update via a "local notification" won't execute. In fact, the badge would only update when the app is in the foreground AND a periodic notification hits (which isn't all that interesting, because with your app in the foreground you're not seeing the tile anyway!).
It sounds like you want to track the number of times the tile was updated by showing that in the badge? (Note that will work only up to 99, the max badge number)
I'd say use a periodic notification for the badge too, and the URL endpoint for that periodic update could be a service that accepts a query string argument uniquely identifying the client. Then your service would generate the correct badge notification for that client, by incrementing a client-specific value that's it's maintaining server side.
Even with this though, you'll have two periodic notifications occurring, one for the tile and one for the badge, and while you can schedule them for the same interval, there's still a potential skew in when they arrive, up to 15 minutes from the desired time.