-
-
Notifications
You must be signed in to change notification settings - Fork 431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
darwin-notifications: Migrate darwin-notifications to electron's native Notifications. #1022
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* This file contains the functions which make use of Main process APIs with already functioning code in Renderer process. | ||
*/ | ||
import {Notification, nativeImage} from 'electron'; | ||
|
||
import fetch from 'node-fetch'; | ||
|
||
export const showDarwinNotification = async ( | ||
event: Electron.IpcMainEvent, | ||
notificationOptions: Electron.NotificationConstructorOptions, | ||
profilePicURL: string | ||
) => { | ||
const profilePic = await getNativeImagefromUrl(profilePicURL); | ||
notificationOptions.icon = profilePic; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to go through the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @andersk the API doesn't support passing the URL directly. That's why we have to go through the cc @priyank-p |
||
|
||
const notification = new Notification(notificationOptions); | ||
notification.show(); | ||
notification.on('reply', (_event, response) => { | ||
event.sender.send('replied', response); | ||
}); | ||
}; | ||
|
||
/* TODO: Migrate url->dataUri conversion part to renderer process | ||
to make use of browser caching of images. | ||
*/ | ||
const getNativeImagefromUrl = async (imageURL: string) => { | ||
try { | ||
const response = await fetch(imageURL); | ||
const buffer = await response.buffer(); | ||
const base64String = buffer.toString('base64'); | ||
const dataUri = `data:image/png;base64,${base64String}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do we know it’s a PNG? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My bad. Referring to this piece of code, I missed this part somehow fiddling around. Adding it back. |
||
const image = nativeImage.createFromDataURL(dataUri); | ||
return image; | ||
} catch (error) { | ||
console.error(error); | ||
return null; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
import {ipcRenderer} from 'electron'; | ||
|
||
import MacNotifier from 'node-mac-notifier'; | ||
|
||
import electron_bridge from '../electron-bridge'; | ||
import * as ConfigUtil from '../utils/config-util'; | ||
|
||
import { | ||
appId, customReply, focusCurrentServer, parseReply | ||
customReply, focusCurrentServer, parseReply | ||
} from './helpers'; | ||
|
||
type ReplyHandler = (response: string) => void; | ||
|
@@ -22,30 +20,21 @@ class DarwinNotification { | |
tag: number; | ||
|
||
constructor(title: string, options: NotificationOptions) { | ||
const silent: boolean = ConfigUtil.getConfigItem('silent') || false; | ||
const {icon} = options; | ||
const profilePic = new URL(icon, location.href).href; | ||
|
||
const profilePicURL = new URL(options.icon, location.href).href; | ||
this.tag = Number.parseInt(options.tag, 10); | ||
const notification = new MacNotifier(title, Object.assign(options, { | ||
bundleId: appId, | ||
canReply: true, | ||
silent, | ||
icon: profilePic | ||
})); | ||
|
||
notification.addEventListener('click', () => { | ||
// Focus to the server who sent the | ||
// notification if not focused already | ||
if (clickHandler) { | ||
clickHandler(); | ||
} | ||
|
||
focusCurrentServer(); | ||
ipcRenderer.send('focus-app'); | ||
const notificationOptions: Electron.NotificationConstructorOptions = { | ||
title, | ||
body: options.body, | ||
silent: ConfigUtil.getConfigItem('silent') || false, | ||
hasReply: true, | ||
timeoutType: 'default' | ||
}; | ||
|
||
ipcRenderer.send('create-notification', notificationOptions, profilePicURL); | ||
ipcRenderer.on('replied', async (_event: Electron.IpcRendererEvent, response: string) => { | ||
await this.notificationHandler({response}); | ||
ipcRenderer.removeAllListeners('replied'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why |
||
}); | ||
|
||
notification.addEventListener('reply', this.notificationHandler); | ||
} | ||
|
||
static requestPermission(): void { | ||
|
@@ -103,8 +92,8 @@ class DarwinNotification { | |
customReply(response); | ||
} | ||
|
||
// Method specific to notification api | ||
// used by zulip | ||
// Method specific to Zulip's notification module, not the package used to create the same. | ||
// If at all the need arises to migrate to another API (or npm package), DO NOT REMOVE THIS. | ||
Comment on lines
+95
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don’t understand this new comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous comment got me believing that this was specific to |
||
close(): void { | ||
// Do nothing | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
node-fetch
doesn’t use the Electron network stack, so this won’t work with custom certificates, proxies, etc. We should useelectron.net
like everything else.