Notification:silent 屬性
注意:此功能在 Web Workers 中可用。
Notification 介面的只讀屬性 silent 指定通知是否應靜默,即,無論裝置設定如何,都不應發出聲音或振動。這透過 Notification() 建構函式的 silent 選項進行控制。
值
一個布林值或 null。如果設定為 true,則通知是靜默的;如果設定為 null(預設值),則尊重裝置的預設設定。
示例
以下程式碼片段會觸發一個靜默通知。建立了一個 options 物件,並透過 Notification() 建構函式響應按鈕點選來觸發通知。程式碼還包括了基本的許可權處理,如果尚未授予通知許可權,則向用戶請求許可權。
js
const btn = document.querySelector("button");
const options = {
body: "No annoying pings or vibrations?",
silent: true,
};
function requestSilentNotification() {
const n = new Notification("Silent notification", options);
console.log(n.silent); // should return true
}
btn.addEventListener("click", () => {
if (Notification.permission === "granted") {
requestSilentNotification();
} else {
Notification.requestPermission().then((permission) => {
if (permission === "granted") {
requestSilentNotification();
} else {
console.log("Notification permission was not granted");
}
});
}
});
規範
| 規範 |
|---|
| Notifications API # dom-notification-silent |
瀏覽器相容性
載入中…