1

일부 이벤트가 발생할 때 알림을 표시하는 웹 사이트를 만들려고합니다. the MDN page for Notifications을 점검했고 Firefox Developer Edition 58.0b7에서 작동하지만 Edge 40.15063.674.0 및 Chrome 62.0.3202.94 (Windows 10)에서 이상한 동작이 발생했습니다.브라우저 간 알림을 표시하는 방법

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width"> 
    <title>test</title> 
</head> 
<body> 
    <script> 
    function notify(str) { 
    if(!("Notification" in window) || Notification.permission === "deneided") { 
    return alert(str); 
    } 
    if (Notification.permission === "granted") { 
    return new Notification("TITLE", {body: str}); 
    } 
    Notification.requestPermission().then(perm => { 
    console.log("request result", perm); 
    console.log("Notification.permission", Notification.permission); 
    if (perm === "granted") { 
     return new Notification("TITLE", {body: str}); 
    } 
    }); 
} 

notify("test"); 
    </script> 
</body> 
</html> 
컴퓨터에있는 파일에서 해당 코드를 저장

와 브라우저를 엽니 다 여기

는 최소한의 완전하고 검증 예입니다.

  • Firefox에서는 요청할 때마다 권한을 부여하고 알림이 표시됩니다.
  • 크롬에서 권한을 부여하면 권한 요청이 "granted"을 반환했지만 Notification.permission은 여전히 ​​"기본"이며 알림이 표시되지 않음을 콘솔에서 볼 수 있습니다. notify을 다시 실행하면 알림을 표시 할 수있는 권한을 묻는 대화 상자가 다시 나타납니다.
  • Edge에서 사용 권한을 부여한 후에 Notification.permissiongranted이지만 새 알림을 만들려고 시도하면 UnknownError이됩니다.

Firefox가 Edge 또는 Chrome에서는 작동하지 않는 이유는 무엇입니까? Chrome에서 최소한 작동하도록하려면 어떻게해야합니까?

답변

0

MDN notification API Docs에서 제공하는 예를 따르면 "대체 예제 : 페이지로드시 실행"에서 오버 헤드가 적은 알림을 표시 할 수 있습니다.

크롬에서 페이지를 새로 고침 한 후 업데이트 Notification.permission 변수를 볼 수

(여기에 조각이 알림 시도를 거부합니다) 로컬 파일에 아래의 코드를 사용해보십시오. 내 코드와 같은 결과 크롬 결과에 코드를 사용

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

 
document.querySelector('#notifyButton').addEventListener('click', showNotification) 
 

 
function showNotification(){ 
 
\t console.log('Clicked!!') 
 
\t var options = { 
 
\t  body: 'this is the body!' 
 
\t } 
 
\t var n = new Notification('this is the title', options); 
 
}
<input id="notifyButton" type="button" name="notify" value="click for notification">

+0

. 알림은 표시되지 않고 페이지를 다시로드 한 후에도 'Notification.permission'은 여전히 ​​"기본값"입니다. – Hawkings