| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/API/Notifications_API/Using_the_Notifications_API | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
: Web Worker .
(Notifications) API . . API .
. .
. API .
IRC . . Slack .
. IndexedDB . , .
. API . / .
. (: ) . . Firefox 72 Safari .
Firefox (, HTTPS) <iframe> .
Notification.requestPermission() . .
Notification.requestPermission().then(function (result) {
console.log(result);
});
. .
Notification.requestPermission();
.
" " .
<button id="enable"> </button>
askNotificationPermission() .
function askNotificationPermission() {
//
function handlePermission(permission) {
//
if (!("permission" in Notification)) {
Notification.permission = permission;
}
//
if (
Notification.permission === "denied" ||
Notification.permission === "default"
) {
notificationBtn.style.display = "block";
} else {
notificationBtn.style.display = "none";
}
}
//
if (!("Notification" in window)) {
console.log(" .");
} else {
if (checkNotificationPromise()) {
Notification.requestPermission().then((permission) => {
handlePermission(permission);
});
} else {
Notification.requestPermission(function (permission) {
handlePermission(permission);
});
}
}
}
. Notification.requestPermission() . (Safari ) (Safari ).
handlePermission() . Notification.permission ( ) . .
: 37 load Notification.requestPermission() ( 274284 ).
Notification.requestPermission() . .
function checkNotificationPromise() {
try {
Notification.requestPermission().then();
} catch (e) {
return false;
}
return true;
}
requestPermission() .then() . true . catch() {} false .
Notification . icon body .
(createNotification() ).
var img = "/to-do-notifications/img/icon-128.png";
var text = '! "' + title + '" .';
var notification = new Notification(" ", { body: text, icon: img });
Firefox Safari ( 4) . . . setTimeout() Notification.close 4 . bind() close() .
setTimeout(notification.close.bind(notification), 4000);
: "close" . . " ." .
onclick, onclose, onerror, onshow . Notification EventTarget addEventListener() .
. ? () .
. . .
HTML .
<button> !</button>
.
window.addEventListener("load", function () {
//
//
if (Notification && Notification.permission !== "granted") {
Notification.requestPermission(function (status) {
if (Notification.permission !== status) {
Notification.permission = status;
}
});
}
var button = document.getElementsByTagName("button")[0];
button.addEventListener("click", function () {
//
// 10
if (Notification && Notification.permission === "granted") {
var i = 0;
// (Firefox ) .
var interval = window.setInterval(function () {
// "! 9"
var n = new Notification("! " + i, { tag: "" });
if (i++ == 9) {
window.clearInterval(interval);
}
}, 200);
}
//
// :
// ""
else if (Notification && Notification.permission !== "denied") {
Notification.requestPermission(function (status) {
// ok
if (status === "granted") {
var i = 0;
// (Firefox ) .
var interval = window.setInterval(function () {
// "! 9"
var n = new Notification("! " + i, { tag: "" });
if (i++ == 9) {
window.clearInterval(interval);
}
}, 200);
}
// alert
else {
alert("!");
}
});
}
//
else {
// alert
alert("!");
}
});
});
.
This page was last modified on 2024 12 17 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |