【问题标题】:Electron - Unsupported Browser for FirebaseElectron - Firebase 不支持的浏览器
【发布时间】:2017-10-03 23:01:04
【问题描述】:

尝试在 firebase 中检索令牌时出现以下错误:

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).

有没有办法解决这个问题?我希望能够在我的 android 设备和这个应用程序之间创建一个消息传递系统。有点像 slack 应用程序。

这是一个代码sn-p:

importScripts('https://www.gstatic.com/firebasejs/4.5.0/firebase-app.js');  
importScripts('https://www.gstatic.com/firebasejs/4.5.0/firebase-messaging.js');  

// Initialize Firebase
var config = {
    apiKey: "myapikey",
    authDomain: "myauthdomain",
    databaseURL: "databaseurl",
    projectId: "projectid",
    storageBucket: "storagebucket",
    messagingSenderId: "senderID"
};
firebase.initializeApp(config);

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);
});

messaging.getToken()
.then(function(currentToken) {
  if (currentToken) 
    console.log(currentToken);
})
.catch(function(err) {
  console.log('An error occurred while retrieving token. ', err);  
});

【问题讨论】:

  • 这个没有更新吗?
  • 我会添加一个答案

标签: javascript firebase electron firebase-cloud-messaging


【解决方案1】:

我不得不转向一个名为 Node WebKit 的框架,它支持 firebase 消息传递。看看那个。它非常简单,与电子非常相似。不幸的是,electron 根本不支持 firebase 消息传递。希望这会有所帮助!

【讨论】:

  • 谢谢,真的很有帮助!我要在 Stackoverflow 上添加一个问题,可能有人解决了这个问题.. 谢谢
【解决方案2】:

我找到了这个库electron-push-receiver。 检查它的文档,效果很好。(如果您使用 Typescript,您可能需要提取一些代码)

重要提示:将此行添加到电子文件中:check this

 webPreferences: {
  nodeIntegration: false,
  preload: __dirname + "/preload.js"  <----- here
}

preload.js

window.ipcRenderer = require('electron').ipcRenderer;

然后在应用内,将 firebase 消息配置替换为:

let { ipcRenderer } = window;
import {
 START_NOTIFICATION_SERVICE,
 NOTIFICATION_SERVICE_STARTED,
 NOTIFICATION_SERVICE_ERROR,
 NOTIFICATION_RECEIVED,
 TOKEN_UPDATED
} from "../constants/electron-push";


ipcRenderer.on(TOKEN_UPDATED, (_, token) => {
  setTokenSentToServer(false);
  sendTokenToServer(token);
});

ipcRenderer.on(NOTIFICATION_RECEIVED, (_, notification) => {
  console.log("NOTIFICATION_RECEIVED", notification);
  let { data } = notification;
  // here you get your data
});
// Listen for service successfully started
ipcRenderer.on(NOTIFICATION_SERVICE_STARTED, (_, token) => {
  console.log("NOTIFICATION_SERVICE_STARTED", token);
  setTokenSentToServer(false);
  sendTokenToServer(token);
});
// Handle notification errors
ipcRenderer.on(NOTIFICATION_SERVICE_ERROR, (_, error) => {
  console.log("NOTIFICATION_SERVICE_ERROR", error);
});
ipcRenderer.send(START_NOTIFICATION_SERVICE, senderId);

【讨论】:

  • @FoxDonut 是的,它订阅了 firebase,只是客户端的代码稍微改变了一点,我什至保留了相同的代码,setTokenSentToServersendTokenToServer...
猜你喜欢
  • 2018-03-27
  • 2017-01-21
  • 2019-06-27
  • 2020-07-11
  • 1970-01-01
  • 1970-01-01
  • 2019-07-06
  • 2012-02-09
  • 1970-01-01
相关资源
最近更新 更多