[javascript] PWA 응용 프로그램에 대한 자바 스크립트를 사용하여 장치 수준 알림을 켜거나 끄는 방법은 무엇입니까?

내 요구 사항은 자바 스크립트를 사용하여 안드로이드 장치에서 장치 수준 알림을 켜거나 끄는 방법입니다 (이 플러그인이 PWA 응용 프로그램을 지원하는 경우 플러그인을 사용할 수는 없습니다).

알림이 해제 된 경우 사용자에게 팝업을 표시해야하므로 알림을받을 알림 기능을 활성화하십시오.

아래 답변은 형사 브라우저 수준 알림에만 해당됩니다. 누구든지 알고 있다면 어떻게해야하는지 정확한 답을 알려주세요. 왜냐하면 나는 거기서 멈췄다.

사용자가 사용 중지하면 알림을 보낼 수없는 경우 여기에서 사용자 사용 이미지를 한 번 확인하십시오.

여기에 이미지 설명을 입력하십시오



답변

이 기능은 잘 문서화 된 것 같습니다 .

function notifyMe() {
  // Let's check if the browser supports notifications
  if (!("Notification" in window)) {
    console.log("This browser does not support desktop notification");
  }

  // Let's check whether notification permissions have alredy been granted
  else if (Notification.permission === "granted") {
    // If it's okay let's create a notification
    var notification = new Notification("Hi there!");
  }

  // Otherwise, we need to ask the user for permission
  else if (Notification.permission !== 'denied' || Notification.permission === "default") {
    Notification.requestPermission(function (permission) {
      // If the user accepts, let's create a notification
      if (permission === "granted") {
        var notification = new Notification("Hi there!");
      }
    });
  }

  // At last, if the user has denied notifications, and you 
  // want to be respectful there is no need to bother them any more.
}


답변