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

Implement notification API #522

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions client/absolute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
20 changes: 11 additions & 9 deletions client/notification/notification.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
43 changes: 31 additions & 12 deletions client/notification/notification_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,42 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <reference path="../../references.ts" />

export default class Notification {
private _notifications: Object = {};
declare var self: ServiceWorkerGlobalScope;

constructor() {
}
declare var navigator: any;

async create(NotificationEvent: Event): Promise<boolean> {
// Not implemented yet
return false;
}
async close(NotificationEvent: Event): Promise<boolean> {
// Not implemented yet
export default class NotificationManager {
private notification: Notification;

constructor() { }

async showNotification(title: string, options: object): Promise<boolean> {
if (!navigator.serviceWorker) {
return false;
}

this.notification = new Notification(title, options);

self.registration.showNotification(title, options);
return false;
}
async processClickEvent(NotificationEvent: Event): Promise<boolean> {
// Not implemented yet

async processClickEvent(event: Event, url: string): Promise<boolean> {
if (!navigator.serviceWorker) {
return false;
}
console.log('[Service Worker] Notification click Received.');

var init = { notification: this.notification };
var myNotificationEvent = new NotificationEvent(event.type, init);

myNotificationEvent.notification.close();

myNotificationEvent.waitUntil(
self.clients.openWindow(url)
);
return false;
}
}
2 changes: 2 additions & 0 deletions references.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference path="node_modules/typescript/lib/lib.webworker.d.ts" />
/// <reference path="node_modules/typescript/lib/lib.es6.d.ts" />