2

无法获得通知权限。FirebaseError:消息:未授予所需的权限,而是取消了。(消息/权限默认)。在我什至可以从通知提示中选择一个选项之前显示在控制台上。通常,此错误应该在从通知提示中选择一个选项后显示,但由于某种原因,它在我什至可以单击提示之前显示,因此通知现在不起作用。尽管它在此之前一直在完美地工作。此代码目前在 chrome 版本 81.0.4044.113 (Official Build) (64-bit) 上运行良好,之前在 firefox 44 上运行良好。

 if (!("Notification" in window)) {
console.error("Notification isn't enabled");
} else if (Notification.permission === "granted") {
console.log("Notification is enabled");
 } else if (Notification.permission !== "denied") {

 // Retrieve an instance of Firebase Messaging so that it can handle background
  // messages.
 firebase.initializeApp(firebaseConfig);
  const messaging = firebase.messaging();
  messaging
  .requestPermission()
  .then(function () {
   // MsgElem.innerHTML = "Notification permission granted." 
    //console.log("Notification permission granted.");

   // get the token in the form of promise
   return messaging.getToken()
  })
  .then(function(token) {
    // print the token on the HTML page
    //TokenElem.innerHTML = "token is : " + token
         $.ajax({
        type:'POST',
        url:'/SaveNotificationToken',
        data:{token : token, _token: "<?php echo csrf_token(); ?>",UserId:             {{auth()->user()->id}} },
        success:function(data){
            //alert(data+"You will receive notiications from clients that viewed your service ");
            $("#msg").html(data);
        }
    }); 

   })
 .catch(function (err) {
  //ErrElem.innerHTML = ErrElem.innerHTML + "; " + err
   console.log("Unable to get permission to notify.", err);
});


}
4

0 回答 0