Is there any way to generate notifications, from an extension?


#1

Hello everyone, I have been developing an extension for firefox, and I would like to know if there is any way to generate notifications from the extension compatible with firefox for mobile?


(Martin Giger) #2

The browser.notifications API also works on Android.


#3

Hello, I have been trying to create a notification that appears on the screen, however it is not shown for any reason that I do not know. Next I attach the script that I use to try to show a notification on the screen:

var title = browser.i18n.getMessage(“notificationTitle”);
var content = browser.i18n.getMessage(“notificationContent”, message.url);
browser.notifications.create({
“type”: “basic”,
“iconUrl”: browser.extension.getURL(“icono.png”),
“title”: title,
“message”: content
});


(Martin Giger) #4

That seems fine, assuming you have the notification permission. A notification should then be shown for Firefox on Android (not a toast, a full notification).


#5

The permissions I use in my extension are the following:

“permissions”: [
“webNavigation”,
:///*”,
“notifications”,
“activeTab”
]

I’ve developed the extension to supposedly work for both firefox pc, and firefox android, so I think I’ve used the correct permissions. Even so, if you have an example, I would appreciate it.

Thank you.


(adilson) #6

@post_rewartip.com, here:


#7

Hello, I’ve tried again to add notifications, but without success. I attach the code that I use:

content-script.js:

function notifyExtension(e) {
var target = e.target;
while ((target.tagName != “A” || !target.href) && target.parentNode) {
target = target.parentNode;
}
if (target.tagName != “A”)
return;

console.log(“content script sending message”);
browser.runtime.sendMessage({“url”: target.href});
}

/*
Add notifyExtension() as a listener to click events.
*/
window.addEventListener(“click”, notifyExtension);

notifications-script.js:

function notify(message) {
console.log(“background script received message”);
var title = browser.i18n.getMessage(“notificationTitle”);
var content = browser.i18n.getMessage(“notificationContent”, message.url);
browser.notifications.create({
“type”: “basic”,
“iconUrl”: browser.extension.getURL(“icono.png”),
“title”: title,
“message”: content
});
}

/*
Assign notify() as a listener to messages from the content script.
*/
browser.runtime.onMessage.addListener(notify);

And finally, in the manifest I have added both the permissions, as the scripts, and background to use (note, the background.js, are other parts of the extension that has nothing to do with notifications, but that also work)

“permissions”: [
“webNavigation”,
:///*”,
“notifications”,
“<all_urls>”,
“tabs”
],
“background”: {
“scripts”: [“jquery-3.1.1.min.js”,“background.js”,“notifications-script.js”,“background2.js”,“background3.js”],
“persistent”: false
},
“content_scripts”: [
{
“matches”: ["<all_urls>"],
“js”: [“content-script.js”]
}
]


#8

Hello,

I’ve been testing the notifications that I mentioned a while ago, and despite having managed to show notifications in both Firefox for PC and Android, I have a problem.

You will see that the notifications are related to a database, that is, if the URL where I am, matches one of the URLs that I have stored in my database, I want you to show me in the form of notification, the data that I have in my database in relation to the URL in which I am.

Is there any way to do it?