问题
How can I update my app's live tile at a fixed interval, for example half a day, using JavaScript?
Moreover, it has to be able to update even though the app itself is not running (like the weather app for example)
EDIT: I want to update it locally without having to connect to the internet. And please give some example in JavaScript, not C# please!
To be more specific, for example, how can I set the tile to update once per day, showing the current date?
回答1:
You can use a background task on a schedule for an interval for anything greater than 15 mins. The tasks are designed to be run by the OS separate to your application, so will be executed if it is not active or open. Here is a detailed blog post on getting started with background tasks: http://www.silverlightshow.net/items/Windows-8-metro-make-your-app-alive-with-background-tasks.aspx
回答2:
If the content on the tile is going to be the date, scheduled tile updates are a good option (see ScheduledTileNotification and the JavaScript Scheduled notifications sample on MSDN). You can schedule a notification to occur once a day with the date as the content.
If you can stand up a web service, periodic updates would be a good option. Using this approach, a tile will be updated on a fixed interval whether or not the app is running. Alternatively, you could use push, or update the tile using a background task.
MSDN has a good article on choosing the right notification delivery mechanism, and links to related code samples: http://msdn.microsoft.com/en-us/library/windows/apps/hh779721.aspx
回答3:
Register a Background Task that executes ever 12 hours and Updates your Tile.
var RegisterBackgroundTask = function (taskEntryPoint, taskName, trigger, condition) {
UnregisterTask(taskName);
Windows.ApplicationModel.Background.BackgroundExecutionManager.requestAccessAsync();
var TaskBuilder = new Windows.ApplicationModel.Background.BackgroundTaskBuilder();
TaskBuilder.name = taskName;
TaskBuilder.taskEntryPoint = taskEntryPoint;
TaskBuilder.setTrigger(trigger);
if (condition !== null) {
TaskBuilder.addCondition(condition);
// If the condition changes while the background task is executing then it will be canceled.
TaskBuilder.cancelOnConditionLoss = true;
}
var task = TaskBuilder.register();
// Remove previous completion status from local settings.
var settings = Windows.Storage.ApplicationData.current.localSettings;
settings.values.remove(taskName);
};
var UnregisterBackgroundTask = function (sTaskName) {
var iter = Windows.ApplicationModel.Background.BackgroundTaskRegistration.allTasks.first();
var hascur = iter.hasCurrent;
while (hascur) {
var cur = iter.current.value;
if (cur.name === sTaskName) {
cur.unregister(true);
}
hascur = iter.moveNext();
}
}
Register Task like this:
RegisterTask(
"App_JS\\LiveTileTask.js",
"Task.LiveTileTask",
new Windows.ApplicationModel.Background.TimeTrigger(15, false),
new Windows.ApplicationModel.Background.SystemCondition(Windows.ApplicationModel.Background.SystemConditionType.internetAvailable)); // 15 min are minimum
In a dedicated File "LiveTileTask.js" you write the Code of your Live Tile Updater:
(function () {
"use strict";
importScripts("//Microsoft.Phone.WinJS.2.1/js/base.js");
var backgroundTaskInstance = Windows.UI.WebUI.WebUIBackgroundTaskInstance.current;
var Notifications = Windows.UI.Notifications;
var tileUpdateManager = Notifications.TileUpdateManager.createTileUpdaterForApplication();
tileUpdateManager.clear();
tileUpdateManager.enableNotificationQueue(true);
for (var i = 0; i < 6; i++) {
var tileWideXml = Notifications.TileUpdateManager.getTemplateContent(Notifications.TileTemplateType.tileSquare150x150Text04),
tileAttributes = tileWideXml.getElementsByTagName("text");
tileAttributes[0].appendChild(tileWideXml.createTextNode("My Live Tile Text No. " + i));
var currentTime = new Date(),
dueTime = new Date(currentTime.getTime() + 30),
tileNotification = new Notifications.ScheduledTileNotification(xmlTile, dueTime),
tileUpdater = Notifications.TileUpdateManager.createTileUpdaterForApplication();
tileUpdater.addToSchedule(tileNotification);
}
backgroundTaskInstance.succeeded = true;
close();
})();
Finally do not forget to register the backgroundtask in your appxmanifest. Under "declarations" select "Background Task" and activate "Timer" and set "Start Page" to "App_JS\LiveTileTask.js". This way you should be able to update your Tile.
回答4:
You use Azure and Push Notifications to send out notifications to all your app users: http://msdn.microsoft.com/en-us/library/windows/apps/hh465460.aspx
回答5:
I recommend you create an Azure Mobile Service. It's really easy and cheap and means you get all of the advantages of a separate push service (instead of a scheduled tile notification on the device) to send your push notifications but without all the work. You can sign up for a free trial if you'd like and create up to 10 mobile services for free. You just create a mobile services and then create a scheduled script that runs every 12 hours and in the script you write a little bit of server-side JavaScript code that does your push. That's just one of the things the Mobile Services gives you. You also get data, authentication, and more. www.windowsazure.com
来源:https://stackoverflow.com/questions/14805223/update-live-tile-at-fixed-interval-without-having-the-app-running