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

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?

The browser.notifications API also works on Android.

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
});

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).

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.

@post_rewartip.com, here:

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”]
}
]

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?

I think you have to learn how to debug:

  • Check for any error output. If there are errors printed, fix their root cause.
  • To check for errors that may not print, or code that just doesn’t behave the way you expect it to, step through your code to see what is actually happening.
  • Build minimal examples. If you think notifications don’t work, don’t rely on
    • the background and content script to start correctly, your click handler to register, your onclick logic to work, the message sending and receiving to work and the notifications to show up. Any of the prevous steps could be broken as well.
    • Instead, write a background script that does nothing but show a notification!

If you can’t get the very last step to work, post your formatted(!) code here, tell us what you expect to happen and what exactly happens instead (or doesn’t).