quickstart-js
quickstart-js copied to clipboard
The script has an unsupported MIME type ('text/plain').
I'm trying to get the registered_token, but I keep getting the following error. How can I fix it?
index.html
<!DOCTYPE html>
<html>
<head>
<!-- favicon.ico 404 (Not Found) -->
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<p id="demo"></p>
<p>A function is triggerled when the button is clicked. The function outputs some text in a p element with id="demo".</p>
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/8.0.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.0.1/firebase-analytics.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.0.1/firebase-messaging.js"></script>
<script>
var firebaseConfig = {
apiKey: "AIzaSyBfLYQieTSNFJVghLmwPidH9eMLJ8sgafA",
authDomain: "push-server-56a52.firebaseapp.com",
databaseURL: "https://push-server-56a52.firebaseio.com",
projectId: "push-server-56a52",
storageBucket: "push-server-56a52.appspot.com",
messagingSenderId: "967213415011",
appId: "AIzaSyBfLYQieTSNFJVghLmwPidH9eMLJ8sgafA",
measurementId: "G-MDVS63TX7X"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
//모든 메세지 서비스 접근 ㅏ능
const messagings = firebase.messaging();
messagings.requestPermission()
.then(function() {
console.log('Have Permission');
return messagings.getToken();
})
.then(function(token){
console.log(token);
})
.catch(function(err){
console.log('Error Occured.',err);
})
// [END on_background_message]
</script>
</body>
</html>
firebase-messaging-sw.js
var firebaseConfig = {
apiKey: "AIzaSyBfLYQieTSNFJVghLmwPidH9eMLJ8sgafA",
authDomain: "push-server-56a52.firebaseapp.com",
databaseURL: "https://push-server-56a52.firebaseio.com",
projectId: "push-server-56a52",
storageBucket: "push-server-56a52.appspot.com",
messagingSenderId: "967213415011",
appId: "1:967213415011:web:ad877f78d7eff7212dd656",
measurementId: "G-MDVS63TX7X"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();
messaging.usePublicVapidKey("BGgFrjLPNrLqke1o7vPLZPcrOGjfPeFVq82N3fzwtRjd37j9LuWc1xxmV7vXhrDpqFK_dgfuznGDRXVzC3fh1Hs")
messaging.getToken().then((currentToken) => {
if (currentToken) {
sendTokenToServer(currentToken);
updateUIForPushEnabled(currentToken);
} else {
// Show permission request.
console.log('No Instance ID token available. Request permission to generate one.');
// Show permission UI.
updateUIForPushPermissionRequired();
setTokenSentToServer(false);
}
}).catch((err) => {
console.log('An error occurred while retrieving token. ', err);
showToken('Error retrieving Instance ID token. ', err);
setTokenSentToServer(false);
});
Hi, could you solve this problem, is it happening to me too?
you have to add a firebase-messaging-sw.js file in your public folder
`importScripts("https://www.gstatic.com/firebasejs/8.2.0/firebase-app.js"); importScripts("https://www.gstatic.com/firebasejs/8.2.0/firebase-messaging.js");
self.addEventListener('notificationclick', function (event) { console.log('On notification click: ', event); });
firebase.initializeApp({ //config });
const messaging = new firebase.messaging.isSupported() ? new firebase.messaging() : null;`
using importScripts('https://www.gstatic.com/firebasejs/9.9.0/firebase-app-compat.js'); importScripts('https://www.gstatic.com/firebasejs/9.9.0/firebase-messaging-compat.js');