K
K
Konstantin Khairov2019-02-10 17:14:19
Push technologies
Konstantin Khairov, 2019-02-10 17:14:19

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);
          });
        }
      });
    });

SendTokenToServer(currentToken); // THIS IS A FUNCTION THAT ACCEPTS A TOKEN AND REQUESTS A PHP SCRIPT TO SAVE TO THE DB. And so it just does not start up to it. I tried to add console.log('test'); right before the function call.
but this did not issue, therefore, until this moment the script does not reach, apparently, it does not fall into the IF condition. Moreover, if this script is opened on a pure html file and run, then everything works fine .... What could be the matter?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question