Question

I have an instant messaging application almost complete the only thing I'm missing are firefox notifications for you. As is the API?

Was it helpful?

Solution

It's not clear if you're after Notifications API or Push Notifications API, so I'll talk about both.

First, the Notification "API" (I'm not sure if it's been renamed to be an API, and I'm afraid it's just known as "Notification" by now). You can use mozNotification to create new notification objects and use show to display them:

var notification = navigator.mozNotification;

var n = notification.createNotification("Title", "Body", "optional_icon.png");
n.show();

On both links, you can get a better understanding what can be done with this.

Be sure you ask for permission to use this (add this to your manifest.webapp):

"permissions": {
    "desktop-notification":{}
}

To make it easier, I've made a demo app. That a look at how it works, and the changes I've made.

Warning: this is different from Web Notifications.

The Push API is already listed on Mozilla Wiki, but it's work in progress AFAIK. You can follow the news on this specific API on Mozilla Wiki, on github (server side stuff) and on the Gecko implementation bug.

There's another API, the SimplePush API, that seems to be working by now. Unfortunately, I don't know much about it. But at least the documentation seems pretty good.

Unfortunately again, I don't know how those APIs relate to W3C's Push API. I'm afraid the Push API is somewhat related to the standard, although I'm not sure of that. I wouldn't rely on any other documentation besides Mozilla's, in the case of these APIs.

OTHER TIPS

For server based push notifications see Simple Push that includes implementation examples. This is available in Firefox OS (/Boot2Gecko) 1.1. but not in 1.0 (which is on all currently released devices). Please also note, that you'll need a real device and it won't work in the emulator.

Simple Push itself is not difficult to implement given the above link. Nevertheless there a couple of things you need to take care about:

  • you need your own server that will trigger Firefox's server on demand for a push
  • the push received on the device does not contain any information (this means you'll have to query needed information after receiving the push)
  • the push received is silent and invisible - that means you'll have to display a desktop notification yourself as described above
  • the push endpoint has to be the same page as your launch_path from the manifest (this means you'll have to put all the logic in one page) due to a bug
  • make sure you'll acquire a wake lock after receiving the push and release it after your work is done - this way the device will not fall to deep sleep and your push processing is paused in the middle of it

It is possible to build similar functionality on Boot2Gecko 1.0 using MozAlarmsManager.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top