Dalisay
Dalisay

Reputation: 1

Rich Notification is not working

I'm currently working a chrome extension that will notify if the getElementId is not in the page.. and unfortunately the rich notification is not showing in contennt script. Should I use Message Passing for this?

load.js

con = document.getElementById('content');

if (con !=null){

     var options = {

    type:"basic",
    title:"Error",
    message:"Error",
    iconUrl:"logo1.png"
}

chrome.notifications.create(options, callback);

function callback()
{

    console.log('yey');
}

}

manifest.json

{
    "manifest_version": 2,

    "name": "CRM Online Chrome Extension",
    "description": "License authentication key for the clients.",
    "version": "1.0",

 "background": {
        "scripts": [
            "background.js"




        ],
        "persistent": true
    },


      "content_scripts":[
{


    "matches":[ "*://*/*",
        "*://*/*"
    ],
    "js":["payload.js","load.js"]

}


    ],

    "browser_action": {
        "default_title": "Sample",
        "default_icon": "logo1.png",
        "default_popup": "popup.html"

    },
    "permissions": [
        "notifications",
        "tabs",
        "*://*/*",
        "*://*/*"






    ]


    // "options_page": "option.html"
}

Upvotes: 0

Views: 120

Answers (1)

Iván Nokonoko
Iván Nokonoko

Reputation: 5118

You have to send a message from your content script to the background page and this latter can create the notification upon the reception of the message.

For example:

background.js

chrome.runtime.onMessage.addListener(function(message){
    if (message.value == "contentPresent"){  //if the message received is the one we sent from our content script
        chrome.notifications.create({  //create notificacion
            type:"basic",
            title:"Error",
            message:"Error",
            iconUrl:"logo1.png"
        }, function(){
              console.log("yey");
        });
   }
});

content.js

if (document.getElementById("content")) {  //if the element with id "content" is found...
        chrome.runtime.sendMessage({value:"contentPresent"});   //send a message to the background script
}

Upvotes: 2

Related Questions