2014-09-01 44 views
0

我正在开发Chrome扩展。nofitication创建的Chrome扩展API不会出现多次

我想在特定条件下显示通知。

我可以显示通知消息一次。

但是,一旦出现通知消息,即使我重装一个网页,它不会再次显示通知消息!

我很困扰这个问题。

请任何人帮助我!

预先感谢:)

manifest.json的

{ 
"name": "test", 
"version": "0.1", 
"manifest_version": 2, 

"permissions": [ 
    "tabs", "notifications", "http://*/*" 
], 

"background": { 
    "scripts": ["background.js"], 
    "persistent": false 
}, 

"content_scripts": [ 
    { 
     "matches": ["http://*/*"], 
     "js": ["myscript.js"] 
    } 
] 

}

myscript.js

var message = { 
    text:"hello" 
} 

chrome.runtime.sendMessage(message, function(response) { 

}); 

backgro und.js

chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) { 

var test = chrome.notifications.create(
    'notification',{ 
     type: 'basic', 
     title: "title", 
     message: request.text, 
     iconUrl:"icon.png" 
     }, 

    function(notificationId) { 

    } 

); 


chrome.notifications.onClosed.addListener(function (notificationId, byUser){ 

     console.log("this doesn't call as well); 
}); 

回答

1

当通知没有点击,它会自动在几秒钟后隐藏(但不是关闭)。再次使用现有的通知ID调用create不会重新创建或显示通知。

致电chrome.notifications.clear在调用chrome.notifications.create之前删除先前创建的ID为“notification”的通知:。

var notificationId = 'notification'; // Whatever 
chrome.notifications.clear(notificationId, function() { 
    chrome.notifications.create(notificationId, ...); 
}); 
+0

谢谢!我在create()方法的回调中调用了clear()方法。这就是为什么它不起作用。现在我有了create()方法之外的clear()方法,它终于可以工作了! – W3Q 2014-09-01 02:41:08