function enableChat() { // initialize Firebase var config = { apiKey: "AIzaSyDnQLFM5Hib9_5AZIapEIVO4NSFTnRYs5E", authDomain: "wbd3-fap.firebaseapp.com", databaseURL: "https://wbd3-fap.firebaseio.com", storageBucket: "wbd3-fap.appspot.com", messagingSenderId: "655114709739", }; firebase.initializeApp(config); const messaging = firebase.messaging(); messaging.requestPermission() .then(() => { console.log("Permission Granted"); }) .catch(function(err) { console.log('Unable to get permission to notify.', err); }); // Get Instance ID token. Initially this makes a network call, once retrieved // subsequent calls to getToken will return from cache. messaging.getToken() .then(function(currentToken) { if (currentToken) { var scope = angular.element(document.getElementsByTagName("body")).scope(); scope.sendTokenToServer(currentToken); scope.$apply(); // sendRegistrationToServer(currentToken); // updateUIForPushEnabled(currentToken); console.log(currentToken); } else { // Show permission request. console.log('No Instance ID token available. Request permission to generate one.'); // Show permission UI. } }) .catch(function(err) { console.log('An error occurred while retrieving token. ', err); }); // Callback fired if Instance ID token is updated. messaging.onTokenRefresh(function() { messaging.getToken() .then(function(refreshedToken) { console.log('Token refreshed.'); console.log(refreshedToken); var scope = angular.element(document.getElementsByTagName("body")).scope(); scope.sendTokenToServer(refreshedToken); scope.$apply(); // Indicate that the new Instance ID token has not yet been sent to the // app server. // setTokenSentToServer(false); // Send Instance ID token to app server. // sendTokenToServer(refreshedToken); // ... }) .catch(function(err) { console.log('Unable to retrieve refreshed token ', err); // showToken('Unable to retrieve refreshed token ', err); }); }); messaging.onMessage(function(payload) { console.log("Message received. ", payload); var scope = angular.element(document.getElementsByTagName("body")).scope(); if (payload.notification.title == "order"){ scope.custId = payload.notification.body; scope.chat = true; scope.cancelFind = false; scope.$apply(); } else if (payload.notification.title == "complete"){ scope.find = false; scope.cancelFind = true; scope.chat = false; scope.$apply(); scope.inOrder(); scope.$apply(); } else { scope.insertMsg(payload); scope.$apply(); } // ... }); }