代码之家  ›  专栏  ›  技术社区  ›  user5803705

Electron-Firebase浏览器不受支持

  •  2
  • user5803705  · 技术社区  · 7 年前

    尝试在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 回复  |  直到 7 年前
        1
  •  1
  •   user5803705 user5803705    6 年前

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

        2
  •  1
  •   challenger    6 年前

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

    check this

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

    preload.js

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

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