Answer the question
In order to leave comments, you need to log in
Why is JS not calling a function?
Hello, I put the script under the PUSH mailing list. Now here's the weird thing going on. On a clean page without the main code, I tested everything works as it should. But when I inserted a subscription on the site in a modal window, that is, the user presses subscribe, a modal window opens and there is a subscribe button.
Here is the script
var config = {
apiKey: "ДАННЫЕ",
authDomain: "ДАННЫЕ",
databaseURL: "ДАННЫЕ",
storageBucket: "ДАННЫЕ,
messagingSenderId: "ДАННЫЕ"
};
firebase.initializeApp(config);
const messaging = firebase.messaging();
var user_token = messaging.getToken();
function Subscribe(){
messaging.requestPermission()
.then(function() {
console.log('Notification permission granted.');
messaging.getToken()
.then(function(currentToken) {
if (currentToken) {
console.log(currentToken);
SendTokenToServer(currentToken); // ЭТО ФУНКЦИЯ КОТОРАЯ ПРИНИМАЕТ ТОКЕН И ДАЕТ ЗАПРОС НА СКРИПТ PHP ДЛЯ СОХРАНЕНИЯ В БД.
} else {
console.log('No Instance ID token available. Request permission to generate one.');
}
})
.catch(function(err) {
console.log('An error occurred while retrieving token. ', err);
showToken('Error retrieving Instance ID token. ', err);
});
// ...
})
.catch(function(err) {
console.log('Unable to get permission to notify.', err);
});
}
messaging.onMessage(function(payload) {
console.log('Message received. ', payload);
navigator.serviceWorker.register('firebase-messaging-sw.js');
Notification.requestPermission(function(result) {
if (result === 'granted') {
navigator.serviceWorker.ready.then(function(registration) {
return registration.showNotification(payload.notification.title, payload.notification);
}).catch(function(error) {
console.log('ServiceWorker registration failed', error);
});
}
});
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question