Alarms let wake up an application in some time in the future.
This API does not show a notification to the user, it just call application code.
Beside permissions, a new entry on messages
is needed (see next section).
"permissions": {
"alarms": {
"description": "Do things behind your back."
}
}
Application manifest.webapp
would need to know the file that will receive the alarm:
"messages": [
{ "alarm": "/index.html" }
]
You need to use the same URL for setting and receiving an alarm. For example, when adding an alarm on
foo.html
orindex.html?foo=bar
, but have{ "alarm": "/index.html" }
in your manifest messages field, you'll never receive the alarm.
alarm
An alarm is dispatched to applications through the System Message API, so applications which want to react to alarms have to register themselves to the alarm
messages.
if (navigator.mozSetMessageHandler) {
navigator.mozSetMessageHandler('alarm', function(mozAlarm) {
// do something about mozAlarm
}
}
Alarms are set using the Navigator.mozAlarms
object. There are two kind of alarms based on the respect of the time zone. In both case it's done using the MozAlarmsManager.add()
method.
if (navigator.mozAlarms) {
const date = new Date("May 15, 2012 16:20:00");;
const body = { reason: "Coffee Time" };
const request = navigator.mozAlarms.add(
date,
'honorTimezone',
body
);
request.onsuccess = () => {
alert(`Alarm set in ${formatDistanceToNow(date)}`);
};
request.onerror = function() {
alert(`Error ${this.error.name} when adding an alarm`);
};
}