Alarm
#
DescriptionThe Alarm API
allows applications to schedule actions to be run in the future. For example, some applications like alarm-clock, calendar or auto-update might need to utilize the Alarm API to trigger particular device behaviors at specified time points.
By itself, the Alarm API just allows to schedule alarms. 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.
Alarms are set using the Navigator.mozAlarms object which is an instance of the MozAlarmsManager interface.
Note: The term alarm in the Alarms API is not the same as an alarm used by the Clock app. The Alarms API wakes up applications, the Clock wakes up humans. The Clock uses the Alarm API to be notified when the time is right to wake up humans.
#
Schedule alarmsThe first things to do when using alarm is to schedule alarms. 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.
Note: If an alarm is not targeted at a specific application, the system will dispatch all the alarms to all the applications listening for alarms.
Note: You need to use the same URL for setting and receiving an alarm. For example, If you invoke
navigator.mozAlarms.add()
onfoo.html
orindex.html?foo=bar
, but have{ "alarm": "/index.html" }
in your manifest messages field, you'll never receive the alarm.
#
Alarms ignoring time zonesThose kind of alarms is dispatched based on the local time of the device. If the user of the device changes its time zone, the alarm will be dispatched based on the new time zone. For example, if a user is in Paris and sets an alarm that should be dispatched at 12 PM CET (Central European Time) and that user travels to San Francisco, the alarm will be dispatched at 12 PM PDT (Pacific Daylight Time).
#
Alarms honoring time zonesThose kind of alarms are dispatched based on the time in the time zone that defines when the alarm has been scheduled. If for some reason, the user of the device changes its time zone the alarm will be dispatched based on the original time zone. For example, if a user is in Paris and set an alarm that should be dispatched at 12pm CET (Central European Time) and if that user travel to San Francisco, the alarm will be dispatched at 3 AM PDT (Pacific Daylight Time).
#
Managing alarmsOnce an alarm is scheduled, it's still possible to manage it.
The MozAlarmsManager.getAll() method will return the complete list of alarms currently scheduled by the application. This list is an Array of mozAlarm objects.
#
mozAlarmThose objects are anonymous JavaScript objects with the following properties:
id
- A number representing the id of the alarmdate
- A Date object representing the scheduled time for the alarmrespectTimezone
- A string indicating if the alarm must respect or ignore the timezone information of the date object. Its value can be ignoreTimezone or honorTimezonedata
- A JavaScript object containing any data that were stored with the alarm
The MozAlarmsManager.remove method is used to unschedule an existing alarm.
#
Handling alarmsAny application can react when an alarm is dispatched by the system. In order to be able to handle any alarms, an application must register itself as an alarm handler. This is done through the System Messaging API in two steps:
First, the applications must include alarm
to the messages property of its application manifest with the URL to the document which registers the callback function to be used when an alarm is dispatched.
Second, the application must bind a callback function with the alarm
message. This is done using the navigator.mozSetMessageHandler() method. This callback function will receive a mozAlarm object containing the data attached to the alarm.
If an application wants to know if there is a pending alarm at the system level, it's possible to use the navigator.mozHasPendingMessage() method with the value alarm.
#
Permissions for the Alarm APIPlease note that while the Alarm API is not privileged or certified, you should still include permissions
and messages
entries in your manifest.webapp
file when including it in an installable open Web app.