Skip to content
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

Provide default implementation of showNotification for Electron #165

Merged
merged 1 commit into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if both onClick and notification are set, won't this result in two subscriptions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I felt that if you went out of your way to work that out then you want both subscribed. Note that the notification object is provided on the default w3c polyfill that redirects to showNotification which is in place for windows 7. With windows 10 the polyfill should be turned off and I'll be working on surfacing this better in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, on a similar topic, won't having show and once called cause an issue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'show' event can be fired multiple times so I only wanted it attached once.

https://electronjs.org/docs/api/notification#event-show

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