Electron - Firebase不支持的浏览器

时间:2017-10-03 23:01:05

标签: javascript firebase electron firebase-cloud-messaging

尝试在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设备和这个应用程序之间创建一个消息传递系统。有点像松弛的应用程序。

以下是代码段:

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

我找到了这个lib Dan Guzman。 检查它的文档,它工作得很好。(你可能需要提取一些代码,以防你使用Typescript)

重要提示:将此行添加到电子文件中:electron-push-receiver

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