webkitnotifications在Chrome

时间:2015-04-25 16:22:00

标签: javascript google-chrome google-chrome-extension

我有一个显示通知的扩展程序,并且在Chrome更新之前一直运行良好,并且不再适用于Chrome。

我应该在此代码上编辑以使其正常工作。 这是我的代码。

deskNoti=webkitNotifications.createNotification(chrome.app.getDetails().name,'You have '+counter+' new messages');
deskNoti.onclick=function(){openPage();this.cancel()
};
deskNoti.show();
	
if(timeNoti){window.setTimeout(function(){deskNoti.cancel();},timeNoti);}

1 个答案:

答案 0 :(得分:1)

webkitNotifications已被删除。直接替换为Notifications API

代码很容易翻译:

// Instead of calling a create function, one calls the "new" operator:
deskNoti = new Notification(
  chrome.app.getDetails().name,
  // Instead of just message text, the second parameter is now an object
  //  with multiple properties. Message text should go into "body" parameter:
  { body: 'You have '+counter+' new messages' }
);

// Instead of .cancel(), the function to close the notification is now .close()
deskNoti.onclick = function() { openPage(); this.close() };

// Notifications are now shown automatically; there is no .show() function
//deskNoti.show();

if(timeNoti) {
  window.setTimeout(function() { deskNoti.close(); }, timeNoti);
}

请考虑使用chrome.notifications API