javascript - Javascript Web Firebase 获取在本地运行但不在公共 IP 中运行的令牌
问题描述
我是 Firebase 的新手,我想弄清楚如何让我的简单网络应用程序获取令牌。
由于这只是原型,我有 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 服务器上配置一些东西吗?开放端口等?
这是屏幕截图:
解决方案
我终于找到了根本原因。
firebase 没有在 somee.com 上运行的原因。因为在 somee.com 我不使用 ssl。但是 000webhost 获得了 SSL。这就是为什么 firebase 在 000webhost 上运行的原因。
因为基于此:
https://github.com/firebase/firebase-js-sdk/issues/1220
firebase 不会在 https 而不是 http 上运行。
推荐阅读
- python - 下载 SEC 数据时出现递归错误
- java - Java SWT删除父级的间距
- android - 我想从 Firestore 读取数据
- python - 如何根据列表元素获取具有某些列和行的新熊猫数据框?
- flask - 如果启用了身份验证,flask-restplus /flask-restx 会自动将 401 响应添加到 Swagger 文档
- azure - 视频索引器显示空白仪表板
- c# - 如何通过传递 MongoDB 的 id 列表来获取其他字段
- r - R:删除边界框的重叠区域并创建一个多边形
- wordpress - 为什么 woocommerce 添加到购物车不适用于我的清漆配置
- java - 在监视私有方法时捕获 org.mockito.exceptions.misusing.WrongTypeOfReturnValue