[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/API/Notifications_API/Using_the_Notifications_API [Back]  [Original]

API - Web API | MDN

MDN Web Docs

View in English Always switch to English

API

: (HTTPS)

:

API API

: To do list via mdn.github.io HEY! Your task "Go shopping" is now overdue [: To do list via mdn.github.io HEY! Your task "Go shopping" is now overdue]

API

IRC Slack

To-do IndexedDB To-do

API 1

() Firefox 72 Safari

Chrome Firefox ( HTTPS) <iframe>

Notification.permission 3

default

granted

denied

Notification.requestPermission()

js
Notification.requestPermission().then((result) => {
  console.log(result);
});

js
Notification.requestPermission((result) => {
  console.log(result);
});

: Notification.requestPermission

To-do "Enable notifications"

html
<button id="enable">Enable notifications</button>

askNotificationPermission()

js
function askNotificationPermission() {
  // Check if the browser supports notifications
  if (!("Notification" in window)) {
    console.log("This browser does not support notifications.");
    return;
  }
  Notification.requestPermission().then((permission) => {
    // set the button to shown or hidden, depending on what the user answers
    notificationBtn.style.display = permission === "granted" ? "none" : "block";
  });
}

2 Notification.requestPermission()

then

Notification (icon) (body)

To-do (createNotification() )

js
const img = "/to-do-notifications/img/icon-128.png";
const text = `HEY! Your task "${title}" is now overdue.`;
const notification = new Notification("To do list", { body: text, icon: img });

close() () ( 4 ) Chrome setTimeout()

js
const n = new Notification("My Great Song");
document.addEventListener("visibilitychange", () => {
  if (document.visibilityState === "visible") {
    // The tab has become visible so clear the now-stale Notification.
    n.close();
  }
});

: API ()

: "close"

Notification

Notifications API Notification 2 :

click

close

error

show

onclick, onclose, onerror, onshow Notification EventTarget addEventListener()

1

HTML

html
<button id="notify">Notify me!</button>
<section id="demo-logs"></section>

js
const demoLogs = document.querySelector("#demo-logs");

window.addEventListener("load", () => {
  const button = document.querySelector("#notify");

  button.addEventListener("click", () => {
    if (Notification?.permission === "granted") {
      demoLogs.innerText += `The site has permission to show notifications. Showing notifications.\n`;
      // If the user agreed to get notified
      // Let's try to send ten notifications
      let i = 0;
      // Using an interval cause some browsers (including Firefox) are blocking notifications if there are too much in a certain time.
      const interval = setInterval(() => {
        // Thanks to the tag, we should only see the "Hi no 9 from MDN." notification
        const n = new Notification(`Hi no ${i} from MDN.`, {
          tag: "soManyNotification",
        });
        if (i === 9) {
          clearInterval(interval);
        }
        i++;
      }, 200);
    } else if (Notification && Notification.permission !== "denied") {
      demoLogs.innerText += "Requesting notification permission.\n";
      // If the user hasn't told if they want to be notified or not
      // Note: because of Chrome, we are not sure the permission property
      // is set, therefore it's unsafe to check for the "default" value.
      Notification.requestPermission().then((status) => {
        // If the user said okay
        if (status === "granted") {
          demoLogs.innerText +=
            "User granted the permission. Sending notifications.\n";
          let i = 0;
          // Using an interval cause some browsers (including Firefox) are blocking notifications if there are too much in a certain time.
          const interval = setInterval(() => {
            // Thanks to the tag, we should only see the "Hi! 9" notification
            const n = new Notification(`Message no ${i} from MDN.`, {
              tag: "soManyNotification",
            });
            if (i === 9) {
              clearInterval(interval);
            }
            i++;
          }, 200);
        } else {
          // Otherwise, we can fallback to a regular modal alert
          demoLogs.innerText += `User denied the permission request.\n`;
        }
      });
    } else {
      // If the user refuses to get notified, we can fallback to a regular modal alert
      demoLogs.innerText += `The site does not have permission to show notifications.\n`;
    }
  });
});

https://live.mdnplay.dev


Web Proxy Viewer  |  New URL  |  Original Page