Я хочу иметь возможность выполнять действие, когда пользователь получает уведомление, когда браузер открыт, а вкладка активна или находится в фокусе. Кажется, я могу найти способ получать уведомления или выполнять действия am, когда вкладка браузера открыта или находится в фокусе. Она хорошо работает, когда браузер находится в фоновом режиме. Я обновил до версии 7.8.0 Firebase и ниже - код, который я использую * 1001. *
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "xxxx",
authDomain: "xxx",
databaseURL: "xxxxx",
projectId: "xxxx",
storageBucket: "xxx",
messagingSenderId: "xxx",
appId: "xxx"
};
// Initialize Firebase
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.onMessage((payload) => {
console.log('Message received. ', payload);
// ...
});
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);
});
}
</script>