【发布时间】:2013-12-23 03:28:29
【问题描述】:
我有一个 Google Chrome 扩展程序正在生产中。该程序的一部分通过 chrome.notifications API 显示通知。我开始用这个逻辑显示通知:
// Expects options: { iconUrl: string, title: string, message: string }
showNotification: function (options) {
// TODO: Future version of Google Chrome will support permission levels on notifications.
if (chrome.notifications.getPermissionLevel) {
chrome.notifications.getPermissionLevel(function (permissionLevel) {
if (permissionLevel === 'granted') {
doShowNotification(options);
}
});
} else {
doShowNotification(options);
}
}
这个逻辑很好用。 getPermissionLevel 功能尚未实现,但将来会实现。因此,我检查它是否已实现,如果没有,则简单地显示通知。
我看到一些客户端出现错误。错误消息指出 chrome.notifications 在以下行中未定义:
if (chrome.notifications.getPermissionLevel) {
带有错误信息:
未捕获的类型错误:无法读取属性“getPermissionLevel” 未定义
这很奇怪,因为这段代码对我来说非常好用,我的测试用例通过了等等。此外,我的 manifest.json 中有以下规则:
"minimum_chrome_version": "29.0.1547.76"
这应该会强制所有客户端使用支持通知的 Google Chrome 版本。根据文档http://developer.chrome.com/extensions/notifications.html,通知在 Chrome 28 中变得稳定:
可用性:自 Chrome 28 起稳定。
此外,我声明了以下权限:
"permissions": [
"contextMenus",
"management",
"notifications",
"storage",
"identity",
"webRequest",
"webRequestBlocking"
]
我正在申请通知权限,并确保用户使用的是当前浏览器版本。
有人知道 chrome.notifications 未定义的任何其他原因吗?我知道我可以简单地确保它被定义,但我想知道发生了什么。谢谢。
【问题讨论】:
标签: javascript google-chrome google-chrome-extension