Error using chrome.notifications.create "Uncaught TypeError: cannot read property" create "from undefined"

Hi, I get an error when calling chrome.notifications.create from inside a function in js application of Chrome. It can be used perfectly from outside the function, but when inside the function I get the following error:

Uncaught TypeError: Unable to read 'create' property from undefined

Here is the code:

document.addEventListener('DOMContentLoaded', function () { document.getElementById('submit').addEventListener('click', submit); }); function submit() { var options = { type:"basic", title:"nameVal", message:"msgVal", iconUrl:"icon.png", }; //notification options set chrome.notifications.create(options,callback); //notification set } function callback() { console.log("Notification succesfull"); //notification confirmed } 

Thanks, I'm a noob when it comes to js and chrome apps, so any help is appreciated :)

+8
source share
3 answers

There are two possible reasons.

  • You are trying to use this from content script . You can't: content scripts are very limited in which Chrome APIs they can call.

    However, content scripts have some limitations. They can not:

    Use chrome. * API, with the exception of:
    extension ( getURL , inIncognitoContext , lastError , onRequest , sendRequest )
    i18n
    runtime ( connect , getManifest , getURL , id , onConnect , onMessage , sendMessage )
    storage

    In this case, you need to delegate this call to the background of the script: send a message from the contents of the script, get it in the background of the script and perform the action.

  • You are trying to call it from a script extension, but you have not declared permission to "notifications" .

    In this case, the fix is ​​trivial - just add permission.

+18
source

Have you added chrome notification rights to your manifest.json?

adding permissions: ["notifications",//other permissions here]

Access rights relate to what is and is not loading in your extension, as well as what you have access to.

+10
source

I have one case for this undefined question when I use chrome.notifications.

 var options = { type: "basic", title: "Extention Title", message: 'Extention Message", iconUrl: "images/icon_86.png" // My Case:Error in iconUrl }; 

But my "icon_86.png" is not in the image folder.

Solution: it will be ok if I use my correct icon.

('icon_86.png', this is in my project root directory)

0
source

All Articles