首页 > 解决方案 > 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 服务器上配置一些东西吗?开放端口等?

这是屏幕截图:

在 Somee.com 中托管时出现屏幕截图错误(基于 Windows 的托管) 000webhost 中的屏幕截图网络工作正常

标签: javascriptfirebasefirebase-cloud-messaging

解决方案


我终于找到了根本原因。

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

因为基于此:

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

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

在此处输入图像描述


推荐阅读