最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chrome extension rich notifications not working - Stack Overflow

programmeradmin2浏览0评论

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"]
}
]
}

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"]
}
]
}
Share Improve this question edited Dec 1, 2013 at 22:02 Dave asked Dec 1, 2013 at 21:18 DaveDave 1892 silver badges14 bronze badges 1
  • 1 Make sure you have permissions set in your manifest (which you prob should include). Also id should be unique. – megawac Commented Dec 1, 2013 at 21:22
Add a ment  | 

1 Answer 1

Reset to default 7

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 municate 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`) 
});
发布评论

评论列表(0)

  1. 暂无评论