Witajcie!!
Mam pewien problem i proszę was o pomoc. Chciałem nauczyć się implementować powiadomienia push w aplikacji internetowej. Korzystam z poradnika jakiegoś hindusa i jestem na tym odcinku:
https://www.youtube.com/watch?v=ZHgr5pQ_prw.
Przejrzałem cały ten odcinek i zrobiłem go dokładnie jak autor i kod wygląda tak(korzystam z biblioteki jquery 3.3.1 min):
Kod: Zaznacz cały
<!--/**
* Created by PhpStorm.
* User: Michał
* Date: 2018-12-26
* Time: 14:41
*/-->
<!DOCTYPE html>
<html>
<head>
<title>Push notification</title>
<script src="jquery-3.3.1.min.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.7.1/firebase.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "AIzaSyDZ56QKkFT33hC3Ee3KyhqwkM0Pmkysg20",
authDomain: "push-v2-7a3d4.firebaseapp.com",
databaseURL: "https://push-v2-7a3d4.firebaseio.com",
projectId: "push-v2-7a3d4",
storageBucket: "",
messagingSenderId: "335978700030"
};
firebase.initializeApp(config);
// Retrieve Firebase Messaging object.
const messaging = firebase.messaging();
messaging.requestPermission().then(function() {
console.log('Notification permission granted.');
// TODO(developer): Retrieve an Instance ID token for use with FCM.
if(isTokenSentToServer()) {
console.log('Token already saved')
} else {
getRegToken();
}
getRegToken();
}).catch(function(err) {
console.log('Unable to get permission to notify.', err);
});
function getRegToken (argument) {
// 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) {
//sendTokenToServer(currentToken);
console.log(currentToken);
setTokenSentToServer(true);
} else {
// Show permission request.
console.log('No Instance ID token available. Request permission to generate one.');
setTokenSentToServer(false);
}
}).catch(function(err) {
console.log('An error occurred while retrieving token. ', err);
showToken('Error retrieving Instance ID token. ', err);
setTokenSentToServer(false);
});
}
function setTokenSentToServer(sent) {
window.localStorage.setItem('sentToServer', sent ? '1' : '0');
}
function isTokenSentToServer() {
return window.localStorage.getItem('sentToServer') === '1';
}
</script>
</head>
<body>
<h1>Push notification v3</h1>
</body>
</html>
"gcm_sender_id": "103953800507" tak jak w tutorialu.
Odpalam stronę na local hoście jak autor i mam taki błąd(oczywiście zezwalaj na wyświetlenie powiadomień push):
http://prntscr.com/m0jz2q
Nie mam pojęcia jak to rozwiązać i szukałem pomocy wszędzie. Zna ktoś może rozwiązanie?
P.S: Mam świadomość , że dwa razy wyświetla mi się funkcja getRegToken();, ale tak było w kursie i żadna z tych funkcji przez ten błąd nie chce wyświetlić mi tokena wymaganego w kolejnych zadaniach