| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/API/Notifications_API/Using_the_Notifications_API | [Back] [Original] |
Get to know MDN better
API API
[: To do list via mdn.github.io HEY! Your task "Go shopping" is now overdue]
API
IRC Slack
API 1
() Firefox 72 Safari
Chrome Firefox ( HTTPS) <iframe>
Notification.requestPermission()
Notification.requestPermission().then((result) => {
console.log(result);
});
Notification.requestPermission((result) => {
console.log(result);
});
To-do "Enable notifications"
<button id="enable">Enable notifications</button>
askNotificationPermission()
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() )
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()
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"
Notifications API Notification 2 :
onclick, onclose, onerror, onshow Notification EventTarget addEventListener()
1
HTML
<button id="notify">Notify me!</button>
<section id="demo-logs"></section>
#demo-logs {
width: 90%;
height: 100px;
background-color: #ddd;
overflow-x: auto;
}
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`;
}
});
});
| Web Proxy Viewer | New URL | Original Page |