Showing more than 1 desktop notification from firefox add-ons

房东的猫 提交于 2019-12-24 01:57:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!