actions for chrome notification not working

时光毁灭记忆、已成空白 提交于 2020-01-05 06:26:14

问题


Hello I am trying to create a chrome notification and I want to add actions onto the notification so the user can select the options. I have tried to run this code but it doesnt work for some reason:

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");
}

the notification works fine without the actions part but I want to be able to have a select in the notification and every time I try and run this script I get this error:

Uncaught SyntaxError: Unexpected identifier

that points to the "actions: [" line

is there something That I am missing?

Any help is apreciated. Thank You <3 !


回答1:


The property "actions" does not exist for notifications. "buttons" is used to add action buttons in the notification.

Also, in "chrome.notifications.create(options, callback);", the parameter list is not correct since the first parameter is "notificationId" which is set to "" in case not used.

Here is an answer which explains well how to use the buttons in chrome notification- 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();
            }
        }
    });


来源:https://stackoverflow.com/questions/52107928/actions-for-chrome-notification-not-working

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