问题
I'd like to show a few desktop notifications triggered from my firefox add-on but it seems like you can only show one at a time, from my testing. I am not sure if there is any official way to do this, since I did not find any mentions in the notifications docs https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/notifications, but I'll take a hacky way, too.
回答1:
Did you say hacky?
var timers = require("sdk/timers");
var {notify} = require("sdk/notifications");
var i=0;
var timerId = timers.setInterval(function() {
notify({
title: 'Famous tities for '+(++i)+'00',
text: "That's famous titles, Mr Connery"
});
if (i>4) timers.clearInterval(timerId);
}, 5000);
They won't stack (at least on OSX), and you might want to check what the default notification time by OS is before setting the delay to 5 seconds, but it's the best I can think of given the limitations of the notification module.
Not that you care, but I just show different notifications in my add-on depending on whether it's one or several; I feel like it's less intrusive.
function showNotif(notifs) {//notifs is an array
var len = notifs.length;
var notifOptions;
if(len>1) {
notifOptions = {
title: len + 'notifications',
text: addonName
}
} else {
notifOptions = {
title: notifs[0].title,
text: notifs[0].text
}
}
notify(notifOptions);
Then you need to add an onClick
to notifOptions
that does something different depending on whether it's for one or many.
来源:https://stackoverflow.com/questions/21288457/showing-more-than-1-desktop-notification-from-firefox-add-ons