From 3b706057b18101f637848eaf3648c359f40ebe8b Mon Sep 17 00:00:00 2001 From: "DESKTOP-MVLSK2J\\Tim" Date: Sun, 12 Nov 2017 17:36:40 +0900 Subject: [PATCH 1/2] Implement notification API --- client/notification/notification_manager.ts | 39 ++++++++++++++------- references.ts | 2 ++ 2 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 references.ts diff --git a/client/notification/notification_manager.ts b/client/notification/notification_manager.ts index ec1e648b..f3fccc47 100644 --- a/client/notification/notification_manager.ts +++ b/client/notification/notification_manager.ts @@ -13,23 +13,36 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +/// + +declare var self: ServiceWorkerGlobalScope; + +declare var navigator: any; export default class Notification { - private _notifications: Object = {}; - constructor() { - } + constructor() { } - async create(NotificationEvent: Event): Promise { - // Not implemented yet - return false; - } - async close(NotificationEvent: Event): Promise { - // Not implemented yet - return false; + async showNotification(title: string, options: object): Promise { + if (!navigator.serviceWorker) { + return false; + } + + self.registration.showNotification(title, options); + return true; } - async processClickEvent(NotificationEvent: Event): Promise { - // Not implemented yet - return false; + + async processClickEvent(event: NotificationEvent, url: string): Promise { + if (!navigator.serviceWorker) { + return null; + } + console.log('[Service Worker] Notification click Received.'); + + event.notification.close(); + + event.waitUntil( + self.clients.openWindow(url) + ); + return null; } } \ No newline at end of file diff --git a/references.ts b/references.ts new file mode 100644 index 00000000..0dbfedb4 --- /dev/null +++ b/references.ts @@ -0,0 +1,2 @@ +/// +/// \ No newline at end of file From f24e0d0a680a217dd4481b7226ed0b0f08aafcca Mon Sep 17 00:00:00 2001 From: "DESKTOP-MVLSK2J\\Tim" Date: Sun, 12 Nov 2017 19:10:13 +0900 Subject: [PATCH 2/2] Implement notification API --- client/absolute.ts | 4 ++-- client/notification/notification.test.ts | 20 +++++++++++--------- client/notification/notification_manager.ts | 20 +++++++++++++------- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/client/absolute.ts b/client/absolute.ts index 422d9437..1c334632 100644 --- a/client/absolute.ts +++ b/client/absolute.ts @@ -15,11 +15,11 @@ */ import PushManager from './push/push_manager'; -import Notification from './notification/notification_manager'; +import NotificationManager from './notification/notification_manager'; import IndexedDB from './indexeddb/indexeddb'; export default class absolute { static push: PushManager = new PushManager(); - static notification: Notification = new Notification(); + static notification: NotificationManager = new NotificationManager(); static indexeddb: IndexedDB = new IndexedDB(); } diff --git a/client/notification/notification.test.ts b/client/notification/notification.test.ts index 1374e6d4..65899c44 100644 --- a/client/notification/notification.test.ts +++ b/client/notification/notification.test.ts @@ -17,15 +17,17 @@ import { } from 'jest'; import absolute from '../absolute'; -test('absolute.notification.create()', async () => { - let NotificationEvent = new Event("click"); - expect(await absolute.notification.create(NotificationEvent)).toBe(false); -}); -test('absolute.notification.close()', async () => { - let NotificationEvent = new Event("click"); - expect(await absolute.notification.close(NotificationEvent)).toBe(false); +test('absolute.notification.showNotification()', async () => { + const title = 'Push Codelab'; + const options = { + body: 'Yay it works.', + icon: 'images/icon.png', + badge: 'images/badge.png' + }; + expect(await absolute.notification.showNotification(title, options)).toBe(false); }); test('absolute.notification.processClickEvent()', async () => { - let NotificationEvent = new Event("click"); - expect(await absolute.notification.processClickEvent(NotificationEvent)).toBe(false); + const event = new Event('click'); + const url = 'https://developers.google.com/web/'; + expect(await absolute.notification.processClickEvent(event, url)).toBe(false); }); \ No newline at end of file diff --git a/client/notification/notification_manager.ts b/client/notification/notification_manager.ts index f3fccc47..d4781c7f 100644 --- a/client/notification/notification_manager.ts +++ b/client/notification/notification_manager.ts @@ -19,7 +19,8 @@ declare var self: ServiceWorkerGlobalScope; declare var navigator: any; -export default class Notification { +export default class NotificationManager { + private notification: Notification; constructor() { } @@ -28,21 +29,26 @@ export default class Notification { return false; } + this.notification = new Notification(title, options); + self.registration.showNotification(title, options); - return true; + return false; } - async processClickEvent(event: NotificationEvent, url: string): Promise { + async processClickEvent(event: Event, url: string): Promise { if (!navigator.serviceWorker) { - return null; + return false; } console.log('[Service Worker] Notification click Received.'); - event.notification.close(); + var init = { notification: this.notification }; + var myNotificationEvent = new NotificationEvent(event.type, init); + + myNotificationEvent.notification.close(); - event.waitUntil( + myNotificationEvent.waitUntil( self.clients.openWindow(url) ); - return null; + return false; } } \ No newline at end of file