Search code examples
javascriptwindows-8triggers

How do I trigger a background task for Windows 8 as soon as app is suspended?


I am new to Windows 8 programming. I am building a countdown timer using JavaScript. I want that even if my app closes, the user is able to get a toast notification telling that the timer's up. So I think I need to use a background task.

I searched a lot for a suitable trigger (to enable the task as soon as app is closed) but didn't find it. Yet it is possible since I saw one countdown timer app in Windows Store do this. Can you tell me a way to implement this?


Solution

  • You want to use a scheduled toast notification, scheduled for the expiration of the timer. This will still be displayed if the app has been closed. See [How to schedule a toast notification][1] in the docs, along with the Scheduled notifications sample, scenario 1. This gives you whatever time resolution you need because you simply tell Windows to display the toast at a given time, and it won't matter whether the app is running, suspended, or terminated.

    Note that the docs page shows the WinRT APIs in Windows.UI.Notifications for this, and the sample uses the "Notifications Extensions" library to build the XML payload (which internally uses WinRT). (Chapter 13 of my free ebook shows all the variations if you want to dig deeper.)

    Either way works; just remember to set Toast Capable to yes in your manifest as described on http://msdn.microsoft.com/en-us/library/windows/apps/hh781238.aspx.