注:
この機能は Web Worker 内で利用可能です。安全なコンテキスト用
この機能は一部またはすべての対応しているブラウザーにおいて、安全なコンテキスト (HTTPS) でのみ利用できます。
permission
は Notification
インターフェイスの読み取り専用プロパティで、ユーザーが現在のオリジンに対してウェブ通知を表示することを許可したかどうか、現在の許可状態を表します。
構文
var permission = Notification.permission;
値
現在の許可を表す DOMString
です。以下の値が可能です。
granted
: 現在のオリジンがシステム通知を表示することについて、ユーザーが明示的な許可を与えている。denied
: 現在のオリジンがシステム通知を表示することについて、ユーザーが明示的に拒否している。default
: ユーザーの決定は分からない。この場合、アプリケーションは許可がdenied
である可能用に動作する。
例
以下のスニペットは、まず通知に対応しているかどうかをチェックし、現在のオリジンで通知を送信するための許可が付与されているかどうかをチェックし、必要であれば許可を要求してから通知を送信したい場合に使用することができます。
function notifyMe() {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
console.log("This browser does not support desktop notification");
}
// Let's check whether notification permissions have alredy been granted
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification("Hi there!");
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== 'denied' || Notification.permission === "default") {
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification("Hi there!");
}
});
}
// At last, if the user has denied notifications, and you
// want to be respectful there is no need to bother them any more.
}
仕様書
仕様書 | 状態 | 備考 |
---|---|---|
Notifications API permission の定義 |
現行の標準 | Living standard |
ブラウザーの互換性
BCD tables only load in the browser