Electron Notifications Help


#1

Hey everyone,

I am new to Electron and to the Atom Discussion page. I had a question regarding notifications through Electron. I have been trying to implement a local notification to test how the look and feel of notifications are in the framework. I keep receiving an error saying:

‘notifications is not defined at EventEmitter..’

Would anyone be able to post up a sample code of how a basic local notification works? Thanks for your help and time, I would really appreciate it.

-Alec


#2

Which OS are you working on?
Notifications isn’t really supported for Windows just yet, and it’s somewhat limited for Linux, see this issue:

However, someone on this other issue says he found a way, using ipc:

webContents.executeJavascript(`
  ipc = require('ipc');
  Notification = function(title, options) {
    ipc.send('notify', {title: title, options: options});
  }
  Notification.permission = 'granted'
`

Not sure, worth trying?


#3

Hey batjko,

Thanks for getting back to me. I will implement this and see what happens. I am running OS X


#4

in which case you should be able to use the Web Notifications API:


#5

Thanks for that. Im still receiving an error saying that Notification is not defined. Shouldn’t this already be built into the electron pre-built package? Thanks in advance


#6

Could you show the code you’re using? It’s much easier for others to help you debug that way :wink:


#7

haha yes I can!

var app= require(‘app’);

app.on(‘ready’, function(){

var BrowserWindow = require(‘browser-window’);
var win = new BrowserWindow({ width: 1150, height: 1920, show: false, ‘node-integration’: false });
win.on(‘closed’, function() {
win = null;
});
function spawnNotification (theBody, theIcon, theTitle){
var options = {
body : theBody,
icon : theIcon
}
var n = new Notification (theTitle, options);
}
win.loadUrl(‘https://app.sightplan.com’);
win.show();
spawnNotification(‘Test Notification’,’/Users/alecdilanchian/Desktop/test1/sightplan.png’, ‘Hello World!’);

})

Am I calling the function in the correct place? I really appreciate all the help


#8

It looks like you are trying to call the notification from the main process but I’m not sure that the Web Notifications API is available outside the renderer process.


#9

Okay got it. Im going to play around with that and see if anything changes. Thanks john!


#10

I think @john is right. You can read more about the separation of main process vs. renderer process in this article.


#11

Thanks for everyones help. It seems like I will need to look into the ipc module. Thanks again!

Im curious if you guys know of any sample codes that I can look into? I’ve been looking everywhere for this information haha.


#12

i have managed to sort the Web API version to work ok.

Here is a ES2015 class i created, and here is how i implemented it, hopefully this might help, but did not require it via the ipc module.

As mentioned before, place this with in your browser js files and not the main process.


#13

Thanks so much for this. I will take a look.