-
Notifications
You must be signed in to change notification settings - Fork 17
SimplePushv1 Developers
Using the Push server as implemented on FirefoxOS 1.1 is really easy.
You, as a app developer, need to take care of the following things:
- Add to your manifest file that you need push permission
- Add a handler to your app when a new push notification arrives
- Use correctly the
navigator.push
interface. - Sending notifications
You need to add a push
message and ask for push
permissions as shown here:
"messages": [
{ "push": "/index.html"},
{ "push-register": "/index.html"}
],
"permissions": {
"push": {}
}
navigator.mozSetMessageHandler("push", function(msg) { alert("New Message with body: " + JSON.stringify(msg)); });
if (window.navigator.mozSetMessageHandler) {
window.navigator.mozSetMessageHandler('push', function(e) {
debug('My endpoint is ' + e.pushEndpoint);
debug('My new version is ' + e.version);
//Remember that you can handle here if you have more than
//one pushEndpoint
if (e.pushEndpoint === emailEndpoint) {
emailHandler(e.version);
} else if (e.pushEndpoint === imEndpoint) {
imHandler(e.version);
}
});
} else {
// No message handler
}
if (navigator.push) {
var req = navigator.push.register();
req.onsuccess = function(e) {
var endpoint = req.result;
debug("New endpoint: " + endpoint );
}
req.onerror = function(e) {
debug("Error getting a new endpoint: " + JSON.stringify(e));
}
} else {
// No push on the DOM
}
You can register()
more than one channels
(or endpoints
) for your application. For example, one for each chat, or football match you are subscribed to.
Once you have your endpoint
, you need to send it to your server, or to the service that will send notifications.
var req = navigator.push.unregister(pushEndPoint)
req.onsuccess = function(e) {
debug('PushEndpoint unregistered');
}
req.onerror = function(e) {
debug('There was an error: ' + JSON.stringify(e));
}
Once you have the endpoint
on your server, to send a notification is as easy as send a HTTP PUT
request to the endpoint with the body version=<version>
. Imagine an endpoint with URL:
https://push.src.openwebdevice.com/v1/notify/abcdef01234567890abcdefabcdef01234567890abcdef
and version 5:
version=5
With curl:
curl -X PUT -d "version=5" https://push.src.openwebdevice.com/v1/notify/abcdef01234567890abcdefabcdef01234567890abcdef
If the server is running correctly, you will get a response with a 200 Status (OK)
and a {}
as a body. If not, a valid JSON explaining the error is sent.
Remember that version should be integer numbers, and incremental. Application will not get new notifications if the new version is lower than the stored on the server and/or device.
You can override your build configured push server
-
adb shell stop b2g
-
adb shell ls /data/b2g/mozilla/
and write down thesomething.default
directory, p.e:zw5oy38a.default
-
adb pull /data/b2g/mozilla/<your_id>.default/prefs.js
-
Edit the prefs.js file, changing the pref
services.push.serverURL
or adding the lineuser_pref("services.push.serverURL", "wss://ua.push.tefdigital.com");
-
adb push prefs.js /data/b2g/mozilla/<your_id>.default/prefs.js
-
adb shell start b2g
And the device will connect to the new push server. Any application that had endpoints for the old server will not work as device will not connect to the old push server.
You can see a simple test app that uses push here: https://github.com/AntonioMA/tsimplepush/