0

I am looking for a way to update the badge value of a MAC application based on information provided in the page loaded by electron.

I am loading the page at startup using the following code in the main.js file.

function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({width: 1280, height: 800, show:false})
// and load the index.html of the app.
mainWindow.loadURL('https://myapp/Home.html');

The page loaded 'https://myapp/Home.html' has an hidden input variable with the number of notifications that would need to be updated on the electon badge

How can I call the variable from the main.js file and update the badge using ?

app.on('ready', app.setBadgeCount(Html_Hidden_Variable))

Please let me know as well is this is the correct way to proceed knowing that I would like to avoid to have to create and additional call the the DB of the application.

Thanks for your help in advance.

2 Answers2

1

You have to use IPC and pass the notification number to main.js and then save it in a variable and use it in your code

jihad.khorfan
  • 335
  • 2
  • 15
1

Here is how i managed to do it.

Get the variable with the number of notifications and send it to the electron application

Home.html

<script>
//setup the electron object to be able to send variable to it 
const ipcRenderer = require('electron').ipcRenderer;

//send the value Html_Hidden_Variable to electron variable CountNotifElectron 
ipcRenderer.send('CountNotifElectron', Html_Hidden_Variable);
</script>

Retreive the variable send and update the badge.

Main.js

const {ipcMain} = require('electron')

//retreive the variable 'CountNotifElectron' with the number of notification
ipcMain.on('CountNotifElectron', function(event, arg) 
{
  //update the value of the badge 
  app.setBadgeCount(arg);
})
})