actions for chrome notification not working

2019-07-24 05:42发布

问题:

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