我是Firebase的新手,我试图弄清楚如何使我的简单Web应用程序获得令牌。
由于这只是原型,所以我有3个文件。
1:init.js
// Initialize Firebase
const config = {
apiKey: "XXXXX",
authDomain: "XXXXX",
databaseURL: "XXXXXXX",
projectId: "XXXXXXXX",
storageBucket: "",
messagingSenderId: "XXXXXX"
}
var defaultApp = firebase.initializeApp(config);
console.log(defaultApp.name);
// Retrieve Firebase Messaging object.
const messaging = firebase.messaging();
messaging.requestPermission().then(function() {
console.log('Notification 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) {
console.log('token 1 : '+currentToken);
var el = document.getElementById("firebaseToken").value = currentToken;
} else {
console.log('token 2 : '+currentToken)
}
}).catch(function(err) {
console.log('An error occurred while retrieving token. ', err);
});
messaging.onMessage(function(payload) {
console.log("Message received. ", payload);
});
2:index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<textarea id='firebaseToken'></textarea>
</head>
<body>
<!-- Firebase App is always required and must be first -->
<script src="https://www.gstatic.com/firebasejs/5.5.9/firebase-app.js"></script>
<!-- Add additional services that you want to use -->
<script src="https://www.gstatic.com/firebasejs/5.5.9/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.5.9/firebase-database.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.5.9/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.5.9/firebase-messaging.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.5.9/firebase-functions.js"></script>
<script src="init.js"></script>
</body>
</html>
3:firebase-messaging-sw.js
importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-messaging.js');
const config = {
apiKey: "XXXXX",
authDomain: "XXXXX",
databaseURL: "XXXXXXX",
projectId: "XXXXXXXX",
storageBucket: "",
messagingSenderId: "XXXXXX"
}
var defaultApp = firebase.initializeApp(config);
console.log(defaultApp.name);
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
const notificationTitle = 'Background Message Title';
const notificationOptions = {
body: 'Background Message body.',
icon: '/itwonders-web-logo.png'
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
问题是,当我在Windows托管,Web应用程序上发布代码时 原型没有获得令牌(我在 somee.com 中托管)。但是它在 000webhost.com (Linux托管)中运行良好,并且在localhost中也可以正常运行。
错误提示:
代码:“消息/不支持的浏览器” 消息:“消息:此浏览器不支持使用Firebase SDK所需的API。(消息/不支持的浏览器)。”
堆栈:“ FirebaseError:消息传递:此浏览器不支持使用Firebase SDK所需的API。(消息/不支持的浏览器)
我必须在Windows服务器上配置某些内容吗?打开端口或其他?
以下是屏幕截图:
答案 0 :(得分:1)
我终于找到了根本原因。
firebase无法在somee.com上运行的原因。 因为在somee.com中,我不使用ssl。但是000webhost获得了SSL。那就是为什么Firebase在000webhost上运行。
因为基于此:
https://github.com/firebase/firebase-js-sdk/issues/1220
firebase无法在https而非http上运行。