【问题标题】:actions for chrome notification not workingchrome 通知的操作不起作用
【发布时间】:2018-08-31 03:09:45
【问题描述】:

您好,我正在尝试创建 chrome 通知,并且我想在通知中添加操作,以便用户可以选择选项。我试图运行这段代码,但由于某种原因它不起作用:

var options = {
    type: "basic",
    title: "Restock",
    message: "Tee Black",
    iconUrl: '/images/Hp_Beta7.png',
    actions: [
        {title: "small", action: "action1"},
        {title: "medium", action: "action2"}
        ]
};

chrome.notifications.create(options, callback);

function callback() {
    console.log("popup done");
}

如果没有操作部分,通知可以正常工作,但我希望能够在通知中进行选择,并且每次尝试运行此脚本时都会收到此错误:

Uncaught SyntaxError: Unexpected identifier

指向“actions: [”行

有什么我想念的吗?

感谢任何帮助。谢谢

【问题讨论】:

  • 操作键前缺少逗号。
  • @wOxxOm 我修复了这个问题,然后我收到了这个错误。未捕获的错误:参数 1 的值无效。属性“操作”:意外的属性。
  • 通知没有actions 键。使用buttons,如"buttons":[{"title":"action1","iconUrl":"button1.png"}]。见documentation

标签: javascript jquery google-chrome google-chrome-extension push-notification


【解决方案1】:

通知不存在属性“actions”。 “buttons”用于在通知中添加动作按钮。

另外,在“chrome.notifications.create(options, callback);”中,参数列表不正确,因为第一个参数是“notificationId”,在不使用的情况下设置为""

这是一个答案,它很好地解释了如何使用 chrome 通知中的按钮-Is there any way to insert action buttons in notification in Google Chrome

background.js

    var myNotificationID = null;
    var options = {
        type: "basic",
        title: "Restock",
        message: "Tee Black",
        iconUrl: "/images/Hp_Beta7.png",
        buttons: [
            {title: "small", iconUrl: "/images/..."},
            {title: "medium", iconUrl: "/images/..."}
        ]
    }
    chrome.notifications.create("", options, function(notificationId){
        myNotificationID = notificationId;
    });

    chrome.notifications.onButtonClicked.addListener(function(notifId, btnIdx) {
        if (notifId === myNotificationID) {
            if (btnIdx === 0) {
                action1();
            } else if (btnIdx === 1) {
                action2();
            }
        }
    });

【讨论】:

  • 可选参数即使是位置参数也可以完全省略,不需要指定空字符串。
猜你喜欢
  • 2019-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
  • 1970-01-01
  • 2016-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多