MatthieuLemoine/electron-push-receiver

Unknown encoding: base64url

vishal-sarvaiya-sub004 opened this issue · 5 comments

// main.js

mainWindow = new BrowserWindow({
    width: userWidth || 1366,
    height: userHeight || 768,
    minWidth: 630,
    minHeight: 375,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      nodeIntegration: false,
      contextIsolation: true,
      enableRemoteModule: false,
    },
    icon: path.join(__dirname, 'assets', 'Icon.png'),
    title: 'Electron FCM Demo',
  })

  await mainWindow.loadURL('http://localhost/ElectronFCMDemo/index.php');
  setupPushReceiver(mainWindow.webContents);

// preload.js

const { contextBridge, ipcRenderer } = require('electron')

contextBridge.exposeInMainWorld('ipcRenderer', {
  send: (channel, data) => {
    ipcRenderer.send("messageFromPreload", "Event >> send message from preload.js to main.js")
    ipcRenderer.send("messageFromPreload", data)
    // Whitelist channels as needed
    ipcRenderer.send(channel, data);
  },
  on: (channel, func) => {
    ipcRenderer.send("messageFromPreload", "Event >> on message from preload.js to main.js")
    ipcRenderer.send("messageFromPreload", channel)
    // set logs to check channel name and data
    ipcRenderer.on(channel, (event, ...args) => func(event, ...args));
  },
  once: (channel, func) => {
    ipcRenderer.send("messageFromPreload", "Event >> once message from preload.js to main.js")
    ipcRenderer.send("messageFromPreload", channel)

    ipcRenderer.once(channel, (event, ...args) => func(event, ...args));
  },
  removeListener: (channel, func) => {
    ipcRenderer.send("messageFromPreload", "Event >> removeListener message from preload.js to main.js")
    ipcRenderer.send("messageFromPreload", channel)

    ipcRenderer.removeListener(channel, func);
  },
  removeAllListeners: (channel) => {
    ipcRenderer.send("messageFromPreload", "Event >> removeAllListeners message from preload.js to main.js")
    ipcRenderer.send("messageFromPreload", channel)
    ipcRenderer.removeAllListeners(channel);
  },
});

// index.php

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Electron FCM Example</title>
</head>
<body>
<h1>Electron FCM Example</h1>

<script>
  (async () => {
    if ('serviceWorker' in navigator) {
      await navigator.serviceWorker.ready;
      let registration = await navigator.serviceWorker.register('./firebase-messaging-sw.js').catch((error) => {
        console.error('Service Worker registration failed:', error);
      });
      console.log('Service Worker registered with scope:', registration.scope);
      console.log('Requesting notification permission...');
    }
  })()
</script>

<script>

  // Listen for service successfully started
  ipcRenderer.on("PUSH_RECEIVER:::NOTIFICATION_SERVICE_STARTED", (_, token) => {
    ipcRenderer.send("messageFromPreload", token)
    console.log(`Electron >> service successfully started: ${token}`);
  });
  // Handle notification errors
  ipcRenderer.on("PUSH_RECEIVER:::NOTIFICATION_SERVICE_ERROR", (_, error) => {
    ipcRenderer.send("messageFromPreload", error)
    console.log('Electron >> notification error', error);
  });
  // Send FCM token to backend
  ipcRenderer.on("PUSH_RECEIVER:::TOKEN_UPDATED", (_, token) => {
    ipcRenderer.send("messageFromPreload", token)
    console.log(`Electron >> token updated: ${token}`);
  });
  // Display notification
  ipcRenderer.on("PUSH_RECEIVER:::NOTIFICATION_RECEIVED", (_, notification) => {
    ipcRenderer.send("messageFromPreload", notification)
    console.log('Electron >> notification received:', notification);
  });
  // Start service
  ipcRenderer.send("PUSH_RECEIVER:::START_NOTIFICATION_SERVICE", "<MY SENDER ID>");
</script>

</body>
</html>

Here, Once my app started, I am getting token but as soon as I get crash
I am getting Error below

TypeError [ERR_UNKNOWN_ENCODING]: Unknown encoding: base64url
    at fromString (buffer.js:454:13)
    at Function.from (buffer.js:308:12)
    at decode (D:\Electron\OriginInfotechHRMSElectron\node_modules\http_ece\ece.js:41:19)
    at parseParams (D:\Electron\OriginInfotechHRMSElectron\node_modules\http_ece\ece.js:254:19)
    at Object.decrypt (D:\Electron\OriginInfotechHRMSElectron\node_modules\http_ece\ece.js:366:16)
    at decrypt (D:\Electron\OriginInfotechHRMSElectron\node_modules\push-receiver\src\utils\decrypt\index.js:21:25)
    at Client._onDataMessage (D:\Electron\OriginInfotechHRMSElectron\node_modules\push-receiver\src\client.js:174:17)
    at Client._onMessage (D:\Electron\OriginInfotechHRMSElectron\node_modules\push-receiver\src\client.js:163:12)
    at Parser.emit (events.js:315:20)
    at Parser._onGotMessageBytes (D:\Electron\OriginInfotechHRMSElectron\node_modules\push-receiver\src\parser.js:233:10)
    at Parser._waitForData (D:\Electron\OriginInfotechHRMSElectron\node_modules\push-receiver\src\parser.js:128:14)
    at Parser._onGotMessageSize (D:\Electron\OriginInfotechHRMSElectron\node_modules\push-receiver\src\parser.js:191:12)
    at Parser._onGotMessageTag (D:\Electron\OriginInfotechHRMSElectron\node_modules\push-receiver\src\parser.js:155:10)
    at Parser._waitForData (D:\Electron\OriginInfotechHRMSElectron\node_modules\push-receiver\src\parser.js:122:14)
    at Parser._onData (D:\Electron\OriginInfotechHRMSElectron\node_modules\push-receiver\src\parser.js:78:12)
    at TLSSocket.emit (events.js:315:20)
    at addChunk (internal/streams/readable.js:309:12)
    at readableAddChunk (internal/streams/readable.js:284:9)
    at TLSSocket.Readable.push (internal/streams/readable.js:223:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23) {
  code: 'ERR_UNKNOWN_ENCODING'
}