Skip to content

Commit

Permalink
Provide default implementation of showNotification for Electron (#165)
Browse files Browse the repository at this point in the history
Simply delegate to Electron.  This still requires a polyfill to be provided (example in app.js) for Windows 7
  • Loading branch information
bingenito authored Jun 11, 2018
1 parent 40a6f72 commit 38b0d6a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/Electron/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,16 @@ export class ElectronContainer extends WebContainerBase {
}

public showNotification(title: string, options?: NotificationOptions) {
throw new TypeError("showNotification requires an implementation.");
const Notification = this.electron.Notification;
const notify = new Notification(Object.assign(options || {}, { title: title }));
if (options["onClick"]) {
notify.addListener("click", options["onClick"]);
}
if (options["notification"]) {
notify.once("show", () => notify.addListener("click", options["notification"]["onclick"]));
}

notify.show();
}

public addTrayIcon(details: TrayIconDetails, listener: () => void, menuItems?: MenuItem[]) {
Expand Down
13 changes: 11 additions & 2 deletions tests/unit/Electron/electron.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,13 @@ describe("ElectronContainer", () => {
Menu: {
buildFromTemplate: (menuItems: any) => { }
},
Notification: (options: any) => {
return {
show: () => {},
addListener: () => {},
once: () => {}
}
},
require: (type: string) => { return {} },
getCurrentWindow: () => { return windows[0]; }
};
Expand Down Expand Up @@ -441,8 +448,10 @@ describe("ElectronContainer", () => {
});

describe("notifications", () => {
it("showNotification throws not implemented", () => {
expect(() => container.showNotification("title", {})).toThrowError(TypeError);
it("showNotification delegates to electron notification", () => {
spyOn(electron, "Notification").and.callThrough();
container.showNotification("title", <any> { onClick: () => {}, notification: { } });
expect(electron.Notification).toHaveBeenCalledWith({ title: "title", onClick: jasmine.any(Function), notification: jasmine.any(Object) });
});

it("requestPermission granted", (done) => {
Expand Down

0 comments on commit 38b0d6a

Please sign in to comment.