Skip to content

Commit

Permalink
profilePic: Add support for profile picture in notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
manavmehta committed Aug 26, 2020
1 parent 038a3de commit adfc7a3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ ${error}`
toggleApp();
});

ipcMain.on('create-notification', (event, notificationOptions: Electron.NotificationConstructorOptions) => {
showDarwinNotification(event, notificationOptions);
ipcMain.on('create-notification', async (event, notificationOptions: Electron.NotificationConstructorOptions, profilePicURL: string) => {
await showDarwinNotification(event, notificationOptions, profilePicURL);
});

ipcMain.on('toggle-badge-option', () => {
Expand Down
27 changes: 23 additions & 4 deletions app/main/ipc-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
/**
* This file contains the functions which make use of Main process APIs with already functioning code in Renderer process.
*/
import {Notification} from 'electron';
import {Notification, nativeImage} from 'electron';

export const showDarwinNotification = (
import fetch from 'node-fetch';

export const showDarwinNotification = async (
event: Electron.IpcMainEvent,
notificationOptions: Electron.NotificationConstructorOptions
notificationOptions: Electron.NotificationConstructorOptions,
profilePicURL: string
) => {
const profilePic = await getNativeImagefromUrl(profilePicURL);
notificationOptions.icon = profilePic;

const notification = new Notification(notificationOptions);
notification.show();

notification.on('reply', (_event, response) => {
event.sender.send('replied', response);
});
};

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}`;
const image = nativeImage.createFromDataURL(dataUri);
return image;
} catch (error) {
console.error(error);
return null;
}
};
5 changes: 2 additions & 3 deletions app/renderer/js/notification/darwin-notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@ class DarwinNotification {
tag: number;

constructor(title: string, options: NotificationOptions) {
// X const profilePic = new URL(options.icon, location.href).href;
const profilePicURL = new URL(options.icon, location.href).href;
this.tag = Number.parseInt(options.tag, 10);
const notificationOptions: Electron.NotificationConstructorOptions = {
title,
body: options.body,
silent: ConfigUtil.getConfigItem('silent') || false,
// X icon: profilePic,
hasReply: true,
timeoutType: 'default'
};

ipcRenderer.send('create-notification', notificationOptions);
ipcRenderer.send('create-notification', notificationOptions, profilePicURL);
ipcRenderer.on('replied', async (_event: Electron.IpcRendererEvent, response: string) => {
await this.notificationHandler({response});
ipcRenderer.removeAllListeners('replied');
Expand Down
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
"dependencies": {
"@electron-elements/send-feedback": "^2.0.3",
"@sentry/electron": "^2.0.0",
"@types/node-fetch": "^2.5.7",
"@yaireo/tagify": "^3.17.8",
"adm-zip": "^0.4.16",
"auto-launch": "^5.0.5",
Expand All @@ -161,6 +162,7 @@
"i18n": "^0.13.2",
"iso-639-1": "^2.1.4",
"nan": "^2.14.0",
"node-fetch": "^2.6.0",
"node-json-db": "^1.1.0",
"semver": "^7.3.2"
},
Expand Down

0 comments on commit adfc7a3

Please sign in to comment.