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

API - Web API | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

API

: (HTTPS) .

(Notifications) API . . API .

. .

[]

. API .

In this article

IRC . . Slack .

. IndexedDB . , .

. API . / .

. (: ) . . Firefox 72 Safari .

Firefox (, HTTPS) <iframe> .

Notification.permission . .

default

.

granted

.

denied

.

Notification.requestPermission() . .

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

. .

js
Notification.requestPermission();

.

" " .

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

askNotificationPermission() .

js
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 ).

requestPermission()

Notification.requestPermission() . .

js
function checkNotificationPromise() {
  try {
    Notification.requestPermission().then();
  } catch (e) {
    return false;
  }

  return true;
}

requestPermission() .then() . true . catch() {} false .

Notification . icon body .

(createNotification() ).

js
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() .

js
setTimeout(notification.close.bind(notification), 4000);

: "close" . . " ." .

Notification .

click

.

close

.

error

.

show

.

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

. ? () .

. . .

HTML .

html
<button> !</button>

.

js
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 feature does not appear to be defined in any specification.


Web Proxy Viewer  |  New URL  |  Original Page