Error when using chrome.notifications.create "Uncaught TypeError: Cannot read property 'create' of undefined"
Solution 1
There are 2 possible causes.
-
You are trying to use this from a content script. You can't: content scripts are very limited in what Chrome APIs they can call.
However, content scripts have some limitations. They cannot:
Use chrome.* APIs, with the exception of:
extension
(getURL
,inIncognitoContext
,lastError
,onRequest
,sendRequest
)
i18n
runtime
(connect
,getManifest
,getURL
,id
,onConnect
,onMessage
,sendMessage
)
storage
In that case, you need to delegate this call to a background script: send a message from the content script, get it in a background script and execute the action.
-
You are trying to call it from an extension script, but did not declare the
"notifications"
permission.In that case the fix is trivial - just add the permission.
Solution 2
Have you added the chrome notifications permissions to your manifest.json?
adding permissions: ["notifications",//other permissions here]
The permissions deals with what is and isnt loaded in your extension, and what you have access too.
pizza1talia
Updated on June 03, 2022Comments
-
pizza1talia almost 2 years
Hi I get an error when calling chrome.notifications.create from inside a function in the js of a chrome app. Can be used fine from outside a function but when within a function I get the following error:
Uncaught TypeError: Cannot read property 'create' of 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 :)
-
erikdstock over 8 yearsThis is better than my answer.
-
pizza1talia over 8 yearsI already had this in the manifest.json file thanks tho. I'm really confused
-
Byren Higgin over 8 yearsJust looked at documentation, I think your callback function needs to have the notificationID parameter like so
function callback(notificationId) {...}
. If that doesnt work, try removing the callback function reference from the create() function so its just...create(options);