Windows Notification in FireFox Addon

I created this:

// Nofitication Function
function myNotification(channelName, title) {
  var notification = browser.notifications.create("id",{
    "type": "basic",
    "iconUrl": browser.extension.getURL("icons/64.png"),
    "title": title,
    "message": "Text",
    "eventTime": 32400000
  });

  browser.notifications.onButtonClicked.addListener(function() {
    // Do something when X (close) button got pressed?!
  });
}
//------------------------------------------------------------------------------

Now I have some problems with it.

1. Problem: eventTime
The event time should keep the Notification alive for 9h.
I tested it with 1, 2, 5 minutes aswell. Wont work. It’s close after ~10-15 sec…

2. Problem: onButtonClicked
I try to get the X (close) button clicked (right top corner of a Notification).
When a user close the Notification with the X-Symbol, it ads a 9h rotation time.
So that the Notification will not apear again in the next 9h.
The same for a click ON the Notification. If someone click the Notification a new browser tab is opened.
And the delay is set to 9h aswell.
I tried the onClosed() method. But this happens also when the Notification disapears without any user interaction (click).
So I can’t use onClosed().
But onButtonClicked() seems only working with a custom button or something.

Here are the resources:

I could use the onClosed() Method instead of onButtonClicked IF I can keep the Notification alive forever (eventTime).

Thanks for your help! <3 :slight_smile:

I can’t find that information in either Chromes of Firefox’s documentation. Are you sure that is what eventTime is supposed to do? In chrome there is requireInteraction, but Firefox doesn’t know that.

AFAIK, in Firefox notifications always close after a couple of seconds. The only thing you can do is open a new one with the same id and content before that happens, which resets the timeout. I don’t know if that fires onClosed for the old notification.
On Fennec (Android) notifications always stay indefinitely.

Firefox doesn’t support onButtonClicked. onClicked fires when the notification body was clicked.


Scroll down to eventTime.
But I figured out why. At the end of the document you find compati. notes.
“Only ‘type’, ‘iconUrl’, ‘title’, and ‘message’ are supported.” for FireFox :-\

Why isn’t this supported in FireFox?!
I always was a big fan of FireFox. But in the last month / years I realise that many things aren’t that great on FireFox, sadly.
I tested other Notifications aswell. Like:
var notification = new Notification(title, options);
But this is not working in FireFox Addon. And does not support anything else too.

Is there a way to report this to FireFox developers that this will be supported any time soon?
Like I know, this isn’t supported for a loooong time.
At the moment im at: v51.0
and this was “supported” at v45 …

According to the docs, eventTime is a timestamp. You can’t use it to control how long the notification is displayed for.

So what’s the eventTime for then?!
Just to have a time when the event poped up?!
Like I understood is it:

“eventTime”: Date.now() + 32400000

would be 9h in the future. For me this would be like keep it up for actual date + X miliseconds longer.
I tested it in chrome. But it isn’t working either:

  var notification = chrome.notifications.create("ID",{
    "type": "basic",
    "iconUrl": chrome.extension.getURL("icons/64.png"),
    "title": title,
    "message": "text bla bla",
    "eventTime": Date.now() + 32400000
  });

I tried to get an event when someone clicks on the X (close) button on the Notification:

  browser.notifications.onButtonClicked.addListener(function() {
    console.log('closed');
  });

But it’s not working in chrome at all.
I guess the “onButtonClicked” is only with custom buttons I’d add to the Notification, dunno.

Exactly, the X closes the Notification, which in turn causes onClosed to fire.

Do you know, how I can keep the Notification alive unlimited?!
I saw a YouTube Notification a day ago, and this notification kept alive forever.
Until I closed / clicked (open link) it manually.
That’s exactly what I want. Then I can use the onClose event right (with the X close button).

This is basically the code I use (made it a bit shorter for better overview):

function myNotification(channelName, title) {
  var notification = browser.notifications.create("online_status",{
    "type": "basic",
    "iconUrl": browser.extension.getURL("icons/64.png"),
    "title": title,
    "message": "Click this message to visit the website!"
  });

  browser.notifications.onClicked.addListener(function() {
    browser.tabs.create({"url": "http://www.url.com/" + channelName});
  });
}

Now I really have to figure out, how to keep this notification alive unlimited.
Until a user interaction (manually closed / clicked by user).

Firefox doesn’t allow unlimited notification. Devs have wanted it for a long time, since days of nsINotificationService which is what is internally used. We should do a bug search to see if they changed their minds though.

You could probably spam browser.notifications.create commands with the exact same arguments every twelve seconds or so. That should keep a single notification with your content open indefinitely, but may have platform dependent side effects.

Also, you probably don’t want to add any listeners inside the function that opens the notification. They will stack up if the listeners are function literals.

Ok, got it to work. Thanks!