2014-08-31 110 views
-2

在我的代碼中,我希望能夠創建通知,但以下代碼無法正常工作。我似乎無法在任何地方找到教程,並且API對我沒有任何意義。這裏是我的代碼,我感謝所有幫助。謝謝!未顯示Chrome擴展程序豐富通知

function notify(string) { 
    chrome.notifications.create(string) 
} 
notify("Testing") 

在我的清單:

"permissions": [ "unlimitedStorage", "tabs", "notifications"] 
+2

您沒有足夠接近創建方法。文檔實際上非常清晰和簡潔。你看這裏? https://developer.chrome.com/extensions/notifications#method-create – Antiga 2014-08-31 04:19:40

回答

4

免責聲明:我不是一個Chrome開發者。

根據文檔,notifications.create方法需要在對象中提供幾個選項,第一個參數是通知的ID。

function notify(title, callback) { 

    var options = { 
     title: title, 
     message: "Message goes here" 
     type: "basic", // Which type of notification to display - https://developer.chrome.com/extensions/notifications#type-TemplateType 
     iconUrl: "someimage.jpg" // A URL to the sender's avatar, app icon, or a thumbnail for image notifications. 
    }; 

    // The first argument is the ID, if left blank it'll be automatically generated. 
    // The second argument is an object of options. More here: https://developer.chrome.com/extensions/notifications#type-NotificationOptions 
    return chrome.notifications.create("", options, callback); 

} 

notify("Testing", function(notification){ 
    // Do whatever you want. Called after notification is created. 
}); 

編輯:競猜需要,那麼所有的參數。上面更新的代碼。這對於簡單的通知應該可以正常工作,但是當你開始做任何先進的事情時,你應該read the docs

+0

未捕獲的錯誤:調用表單notifications.create(字符串,對象)不匹配定義notifications.create(字符串notificationId,對象選項,函數回調) – Sam 2014-08-31 04:25:31

+0

這就是我運行代碼時得到的錯誤 – Sam 2014-08-31 04:37:05

+0

應該是'chrome.notifications.create(options,callback);' – Xan 2014-08-31 22:54:28

相關問題