gpt4 book ai didi

Javascript Web Firebase获取在本地运行但不在公共(public)IP中运行的 token

转载 作者:行者123 更新时间:2023-12-03 00:41:28 26 4
gpt4 key购买 nike

我是 Firebase 的新手,我正在尝试弄清楚如何让我的简单网络应用获得 token 。

由于这只是原型(prototype),所以我有 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 应用程序)上发布代码时原型(prototype)没有获得 token (我托管在somee.com)。但它在 000webhost.com(Linux 托管)中运行良好,并且在本地主机中运行也没有问题。

错误提示:

code: "messaging/unsupported-browser" message: "Messaging: This browser doesn't support the API's required to use the firebase SDK. (messaging/unsupported-browser)."

stack: "FirebaseError: Messaging: This browser doesn't support the API's required to use the firebase SDK. (messaging/unsupported-browser)

我必须在 Windows 服务器上配置一些东西吗?开放端口等?

这是屏幕截图:

Screenshot error when hosted in Somee.com (Windows based hosting) Screenshot web work fine in 000webhost

最佳答案

我终于找到了根本原因。

Firebase 未在 somee.com 上运行的原因。因为在 somee.com 中我不使用 ssl。但 000webhost 有 SSL。这就是为什么 firebase 在 000webhost 上运行。

因为基于此:

https://github.com/firebase/firebase-js-sdk/issues/1220

firebase 不会在 https 而不是 http 上运行。

enter image description here

关于Javascript Web Firebase获取在本地运行但不在公共(public)IP中运行的 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53447013/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com