2

I'm creating a Chrome extension for the first time.

I've used chrome.notifications.create in it, but it does not work! This is my code:

chrome.storage.sync.get('refresh_time',function(items){
    status = $('td.c').next().html();
    if (status.trim() == 'SomeText') {
        alert('Works');
        var opt = {
            type: "basic",
            title: "Project1",
            message: "This is my first extension.",
            iconUrl: "icons/icon-128.png"
        };
        chrome.notifications.create('statusChanged', opt, function(){});
    }
})

I get the alarm after execution, but chrome notification does not work! May you tell me what is wrong in my code?

By the way, I used the code below in my manifest file.

"permissions" : [
    "storage",
    "notifications",
    "tabs"
]
hichris123
  • 10,145
  • 15
  • 56
  • 70
Mohammad Saberi
  • 12,864
  • 27
  • 75
  • 127

5 Answers5

5

I think you need to make iconUrl a chrome extension path, something like this:

iconURL: chrome.runtime.getURL("icons/icon-128.png")
tomka
  • 53
  • 1
  • 3
3

I think that the code is good. However, I guess that you need to confirm the iconUrl path. If the file specified by the path does not exist, the notification will not be displayed. At the time, you may see the following error message on the console tab of the DevTools:

Unchecked runtime.lastError while running notifications.create: Unable to download all specified images.

You need to specify the valid path for the iconUrl value.

Yoichiro Tanaka
  • 835
  • 7
  • 14
2

enter image description here

For me, the notifications were disabled on windows. Make sure that "Get notifications from apps and other senders" is enabled.

Stefan
  • 667
  • 7
  • 17
  • I'd somehow turned off specifically notifications from Chrome and nothing else, go figure. Thanks for saving me even more time scratching my head over code that was actually working as intended – Hannah S. May 16 '22 at 20:58
0

Probably, you need to modify the image path as:

iconUrl: "../icons/icon-128.png"
Vishal Kumar
  • 1,290
  • 14
  • 15
0

1. iconUrl path

Your iconUrl SHOULD BE RELATIVE to your manifest.json file.

2. Unique notification ID

Chrome notification should have unique ID. I'd suggest you to use like below

/* Set unique ID for each notification */
chrome.notifications.create(`notification-${Date.now()}`, {
    type: 'basic',
    iconUrl: './logo.png', // <------ this should be relative to your manifest.json file.
    title: 'This is a notification title',
    message: 'This is a notification message',
})
Seiwon Park
  • 158
  • 1
  • 8