Wakeups let your Alloy app schedule itself to launch at a future time, even if it isn't running. When the wakeup fires, the system launches your app and you can read the wakeup details to know why it was started.
Platform Support: Wakeups are available on Emery (Pebble Time 2) and Gabbro (Pebble Time 2 round).
Import the WakeUp class and call schedule() with a timestamp (in
milliseconds, as returned by Date.now()), a numeric cookie of your choice,
and whether the user should be notified if the wakeup is missed:
import WakeUp from "pebble/wakeup";
// Fire 3 seconds from now
const id = WakeUp.schedule(Date.now() + 3000, 12345678, false);
console.log(`Scheduled WakeUp id ${id}`);
schedule() returns an id you can later use to query or cancel the wakeup.
Persist the id (for example with localStorage) if you need it across
launches.
| Method | Description |
|---|---|
WakeUp.schedule(time, cookie, notifyIfMissed) |
Schedule a wakeup at time (ms). cookie is a number passed back to your app. notifyIfMissed shows a missed-event notification if the wakeup couldn't fire. Returns an id. |
WakeUp.cancel(id) |
Cancel a previously scheduled wakeup. |
WakeUp.query(id) |
Return a { time, scheduled } object describing the wakeup - time is the scheduled time in ms and scheduled is true if it is still pending - or a falsy value if it no longer exists. |
When your app is launched by a wakeup, watch.wake is set and contains the
wakeup's id and cookie:
console.log(`Launch reason ${watch.launch.reason}, arguments ${watch.launch.arguments}`);
if (watch.wake) {
console.log(`Launched by wakeup id ${watch.wake.id}, cookie ${watch.wake.cookie}`);
}
If a wakeup fires while your app is already running, it is delivered as a
"wakeup" event on watch:
watch.addEventListener("wakeup", wake => {
console.log(`wakeup id ${wake.id} occurred while running`);
WakeUp.cancel(wake.id);
});
Because a wakeup id outlives a single launch, it's good practice to validate
a stored id on startup and discard it if the wakeup is gone or stale:
const id = localStorage.getItem("wakeid");
if (id) {
const wakeup = WakeUp.query(id);
if (!wakeup) {
// The wakeup no longer exists
localStorage.removeItem("wakeid");
} else if (wakeup.time < Date.now()) {
// The wakeup is in the past; cancel it
WakeUp.cancel(id);
localStorage.removeItem("wakeid");
}
}
See the
hellowakeup example
for a complete project.