【问题标题】:chrome.notifications undefined intermittentlychrome.notifications 间歇性未定义
【发布时间】: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


    【解决方案1】:

    我能想到的唯一原因是该 API 在 Linux 中不受支持。

    the docs

    注意:此 API 目前可在 ChromeOS、Windows 和 Mac 上使用。

    我没有尝试过使用最新版本,但我知道 Linux 上以前的 Chrome 版本存在问题(例如,没有显示按钮等)。

    我不确定它目前是否会以不同的方式影响不同的发行版。无论如何,检查失败客户端的 Chrome 版本和操作系统是个好主意。

    【讨论】:

    • 这听起来非常合理。谢谢你。我今天会调查一下,一旦我确认我会接受评论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 2014-05-03
    • 2019-07-27
    • 2011-01-23
    相关资源
    最近更新 更多