Update live tile at fixed interval without having the app running

一笑奈何 提交于 2019-12-02 02:49:13

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

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

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!