Chrome extension rich notifications not working

google-chrome, google-chrome-extension, javascript

Solution

It seems like you are trying to access the `chrome.notifications` API from a content script. But it is not available for content scripts, so you'll need to create the notification in the background page.

If you need to pass specific data to be displayed in the notification, you can use Message Passing to communicate between the content script and the background page. E.g.:

/* In content script */
chrome.runtime.sendMessage({
    from: sBuffer[0],
    body: sBuffer[2]
});

/* In background page */
chrome.runtime.onMessage.addListener(function(msg, sender) {
    /* Verify the message's format */
    (msg.from !== undefined) || return;
    (msg.body !== undefined) || return;

    /* Create and show the notification */
    // ...your notification creation code goes here
    // (replace `sBuffer[0]`/`sBuffer[2]` with `msg.from`/`msg.body`) 
});

Problem

I have an chrome-extension that i want to use the new rich notifications. I'm trying to implement the following: ``` var opt = { type: "basic", title: "New message from " + sBuffer[0] + ":", message: sBuffer[2], iconUrl: getUserIcon(sBuffer[0]) }; chrome.notifications.create("",opt,function(){}); ``` But no matter what i do, i get the following error: `Uncaught TypeError: Cannot call method 'create' of undefined` I went into `chrome://flags` and set everything with 'notifications' in it to enabled... I'm running chrome 31.0.1650.57 m. This works fine: ``` var notification = webkitNotifications.createNotification( getUserIcon(sBuffer[0]), "New message from " + sBuffer[0] + ":", sBuffer[2] ); notification.show(); ``` It's not pretty, but it works (the icon is tiny even though it's a high-res image... Is there any way i can make the image icon bigger?) BTW, i've got the notifications permission in my manifest. Thanks,Dave EDIT: Included manifest ``` { "manifest_version": 2, "name": "Notifier", "description": "This extension will listen the the JS on the page and popup notifications", "version": "0.1", "permissions": [ "background","notifications" ], "content_scripts": [ { "matches": ["http://MY_WEB_SITE"], "js": ["Notifier.js"] } ] } ```

Original source