Created
March 7, 2019 03:27
-
-
Save sarfarazansari/069b2d5852739e3d0b17305bddca2b2c to your computer and use it in GitHub Desktop.
show electron app notification according to OS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* author: [email protected] | |
* purpose: show native notifications accroding to os | |
*/ | |
import { ipcMain, BrowserWindow } from 'electron'; | |
import * as notifier from 'node-notifier'; | |
import * as path from 'path'; | |
import * as elog from 'electron-log'; | |
function handleRes(e: any, data: any, err: any, response: string, metadata: any) { | |
if (err) { | |
elog.info('error from notification', err); | |
return; | |
} | |
let window = BrowserWindow.getAllWindows(); | |
elog.info('notification', response); | |
// commented due to default focus on app in ubuntu when notification recieved | |
// if (!response) { | |
// if (window[0]) { | |
// window[0].focus(); | |
// } | |
// e.sender.send('push-info-recieved', data); | |
// return; | |
// } | |
if (response && response === 'activate') { | |
if (window[0]) { | |
window[0].focus(); | |
} | |
e.sender.send('push-info-recieved', data); | |
} | |
} | |
ipcMain.on('trigger-push-notification', (e: any, data: any) => { | |
let opts: any = { | |
title: data.title, | |
message: data.message.content, | |
icon: path.join(__dirname, '/assets/icon/icon.png'), | |
sound: true, | |
timeout: 5 | |
}; | |
if (process.platform === 'darwin') { | |
const NotificationCenter = require('node-notifier').NotificationCenter; | |
const mac = new NotificationCenter({withFallback: true}); | |
opts.appIcon = 'https://kiss-uploads.sokt.io/H1TeBMgrX_icon.png'; | |
opts.icon = 'https://kiss-uploads.sokt.io/H1TeBMgrX_icon.png'; | |
opts.sound = 'Tink'; | |
mac.notify(opts, (err: any, response: string, metadata: any) => { | |
handleRes(e, data, err, response, metadata); | |
}); | |
} else { | |
notifier.notify(opts, (err: any, response: string, metadata: any) => { | |
handleRes(e, data, err, response, metadata); | |
}); | |
} | |
/* | |
// response: string 'timeout' | 'closed' | 'replied' | 'activate' | |
// response: undefined in case of windows | |
{ | |
deliveredAt: '2018-08-03 15:58:47 +0530', | |
activationType: 'replied', | |
activationAt: '2018-08-03 15:59:04 +0530', | |
activationValue: 'Am replying 😊 ' } | |
*/ | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment