System messages are messages from the system, they are sent by the system based on their types, for example, a system message with name alarm is sent to its subscriber when a timer goes off; a system message with name sms-received is sent when the system receives an sms message. SystemMessage API gives applications the ability to subscribe system messages of particular names, and events
SystemMessageEvent will dispatched to its registered ServiceWorker in different scenarios.
To subscribe and receive system messages, an app has to have an active service worker.
First, we need to specify the registration info of service worker in manifest, the format is as follows
options object is optional, as defined in ServiceWorkerContainer.register()#Syntax, for example:
In the most case the above example is good enough.
Second, subscribe the names of system messages in manifest.
Since now that system messages are dispatched to service worker, the field of
target page ("/index.html" in the above example) in gecko48 does not affect the receiving of system messages.
Apps can use
SystemMessageManager.subscribe() to subscribe system messages on an active service worker.
systemMessageManager is a property of
ServiceWorkerRegistration, which returns a reference to the
System messages are delivered to the
ServiceWorkerGlobalScope.onsystemmessage event handler, in the format of
- The type of this system message.
- Returns an object
SystemMessageDatawhich wraps the details of this system message.
- Extracts the message data as a JSON object, not available for activity messages.
- Only available for activity messages, details stated in
SystemMessageService provides an interface for module developers to send system messages to target applications, or broadcasting messages to all subscribers.