What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or focused. I can seem to figure out a way to receive notification or perform am action while browser tab is open or focused, It works well when browser is at the background.I upgraded to firebase version 7.8.0 and below is the code am using
<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: },
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>
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire