This Firefox addon-sdk module implements a SimplePush client to allow receiving asynchronous push notifications.
Simply copy fapush.js
into the ./lib
directory of your addon
The FAPush API is very similar to the navigator.push API, with a few differences:
- the service is obtained with
var push = require('./fapush.js')
instead ofvar push = window.navigator.push
- the service does not start automatically and must be launched using the
init(...)
method - notifications are received in a callback passed to the
init(...)
method - the service can be stopped using a
destroy()
method
options
is an object that may hold the following members:
onmessage
: a callback function receiving 2 parameters:type
: either'push'
: regular push notification message- or
'push-register'
: the client must re-register notifications
message
: the message content. Iftype
is'push'
,message
containspushEndpoint
andversion
.
serverUrl
: the push server to connect to (defaults to'wss://push.services.mozilla.com'
)retryTimeout
: time before reconnecting to the push server in case the web socket closes (default10000
, 10 seconds)usePing
: keep alive using ping messages (defaulttrue
)pingTimeout
: time of inactivity before sending a ping message to the server (default1800000
, 30 minutes)pongTimeout
: time to expect answer to ping (default10000
, 10 seconds)
Close push server socket and clear all used resources. Existing channels are not unregistered to the push server and can be reused in a future session.
See PushManager.registrations documentation.
See PushManager.register documentation.
See PushManager.unregister documentation.
var push = require('fapush.js');
var endpoint = null;
push.init({
onmessage: function(type, message) {
if(type=='push' && message.pushEndpoint==endpoint) {
// do something with the notification like asking your server what the notification is about
// and take appropriate action
} else if(type=='push-register') // the server has forgotten us, we need to re-register
RegisterPush();
}
});
function RegisterPush() {
var req = push.register();
req.onsuccess = function(e) {
endpoint = e.target.result;
// tell our app server about the endpoint to be used to notify this client
}
req.onerror = function(err) {
console.warn("Something went wrong",err);
}
}
var req = push.notifications();
req.onsuccess = function(e) {
var regs = e.target.result;
if(regs.length==0)
RegisterPush();
else {
endpoint = regs[0].pushEndpoint;
// we keep it simple here and only use one notification channel
// so we unregister other existing channels (unlikely to exist with this sample code)
for(var i=1;i<regs.length;i++)
push.unregister(regs[i].pushEndpoint);
}
}
req.onerror = function(err) {
console.warn("Something went wrong",err);
}
This module is released under the Mozilla Public License 2.0
Original author: Michel Gutierrez (mig@downloadhelper.net)