Chrome瀏覽器擴展開發系列之十:桌面通知Notification


Desktop Notification也稱為Web Notification,是在Web頁面之外,以彈出桌面對話框的形式通知用戶發生了某事件。Web Notification於2015.9.10成為W3C推薦標准,網址https://www.w3.org/TR/notifications/。每個通知對話框都包括title, direction, language和origin。通知對話框還可以有body, tag, icon URL和icon image。

通知必須獲得用戶的授權才能夠顯示,從而避免非用戶期望的通知干擾用戶。對通知的授權有三種,分別由字符串”default”(用戶未顯式授權,同denied), ”denied”, ”granted”表示。

由於通知的顯示與瀏覽器的實現有關,與用戶的授權有關,所以在使用桌面通知之前,往往要檢查瀏覽器和用戶的授權,示例如下:

function checkNotification(){

if (!("Notification" in window)) {

alert("This browser does not support desktop notification");

}

// check whether notification permissions have alredy been granted

else if (Notification.permission === "granted") {

// If it's okay let's create a notification

new Notification("Granted!");

}

// Otherwise, ask the user for permission

else if (Notification.permission !== 'denied') {

Notification.requestPermission(function (permission) {

// If the user accepts, let's create a notification

if (permission === "granted") {

new Notification("Request granted!");

}

});

}

}

Chrome瀏覽器的chrome.notifications.* API能夠基於模板創建桌面通知,並在操作系統右下角的托盤中顯示通知。

要在Chrome瀏覽器擴展中使用通知,首先要在manifest.json文件中聲明notifications的權限如下:

{

"permissions": [

"notifications"

],

}

chrome.notifications.TemplateType是枚舉類型,枚舉值如下:

枚舉值

注釋

"basic"

默認模板

icon, title, message, expandedMessage位於兩個button之上

"image"

icon, title, message, expandedMessage, image位於兩個button之上

"list"

icon, title, message, items位於兩個button之上

"progress"

icon, title, message, progress位於兩個button之上

chrome.notifications. PermissionLevel是枚舉類型,枚舉值如下:

枚舉值

注釋

"granted"

默認值,用戶授權顯示通知

"denied"

用戶未授權顯示通知

chrome.notifications.NotificationOptions對象的屬性如下:

屬性名

類型

必選/可選

注釋

type

TemplateType

可選

通知的類型,

chrome.notifications.create()創建通知時必選

title

string

可選

通知的標題,

chrome.notifications.create()創建通知時必選

message

string

可選

通知的主體內容,

chrome.notifications.create()創建通知時必選

contextMessage

string

可選

通知的備選內容

buttons

array of object

可選

該數組最多2個元素,分別代表2個button。

每個button對象都有title和iconUrl兩個屬性(string類型),其中iconUrl屬性可選

appIconMaskUrl

string

可選

應用圖標URL的掩碼,用以規范URL

iconUrl

string

可選

圖標的URL

imageUrl

string

可選

"image"類型的通知的圖片的URL

priority

integer

可選

優先級,有效范圍[-2,2],默認0

eventTime

double

可選

通知的時間戳,單位ms

items

array of object

可選

該數組中的每個元素代表一個通知。

每個通知對象都有title和message兩個屬性(string類型)

progress

integer

可選

當前的進度,有效范圍[0,100]

isClickable

boolean

可選

通知窗口是否響應點擊事件

chrome.notifications API中的常用方法:

· 創建並顯示通知

chrome.notifications.create(

string notificationId,

NotificationOptions options,

function(string notificationId) {...}

)

其中屬性說明如下:

屬性名

類型

必選/可選

注釋

notificationId

string

可選

通知的標識符。

如果未設置或設置為””,則自動生成唯一標識符;

如果設置的值與已有的通知相同,則替換已有的通知

options

NotificationOptions

必選

通知的具體內容

· 更新已有的通知

chrome.notifications.update(

string notificationId,

NotificationOptions options,

function (boolean wasUpdated) {...}

)

其中屬性與create()類似。

· 清除指定的通知

chrome.notifications.clear(string notificationId, function(boolean wasCleared) {...})

· 獲取所有通知

chrome.notifications.getAll(function(object notifications) {...})

 

最后介紹Chrome擴展中background與notification之間的互操作問題。

在處理通知的JavaScript腳本文件中,可以訪問background頁面的方法如下:

chrome.extension.getBackgroundPage().doThing();

在background頁面的JavaScript腳本文件中,可以訪問通知的JavaScript腳本文件中的方法如下:

chrome.extension.getViews({type:"notification"}).forEach(function(notificationWindow) {

notificationWindow.doOtherThing();

});


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM