Skip to content

Commit

Permalink
feat(web): add basic LocalNotifications plugin. Closes ionic-team#1355
Browse files Browse the repository at this point in the history
  • Loading branch information
mlynch committed May 19, 2019
1 parent ea7e1f7 commit 54d3a1b
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/src/web-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from './web/clipboard';
export * from './web/filesystem';
export * from './web/geolocation';
export * from './web/device';
export * from './web/local-notifications';
export * from './web/share';
export * from './web/modals';
export * from './web/motion';
Expand Down
118 changes: 118 additions & 0 deletions core/src/web/local-notifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { WebPlugin } from './index';

import {
LocalNotificationsPlugin,
LocalNotificationEnabledResult,
LocalNotificationPendingList,
LocalNotificationActionType,
LocalNotification,
LocalNotificationScheduleResult
} from '../core-plugin-definitions';

import { PermissionsRequestResult } from '../definitions';

export class LocalNotificationsPluginWeb extends WebPlugin implements LocalNotificationsPlugin {
private pending: LocalNotification[] = [];

constructor() {
super({
name: 'LocalNotifications',
platforms: ['web']
});
}

sendPending() {
const toRemove: LocalNotification[] = [];
const now = +new Date;
this.pending.forEach(localNotification => {
if (localNotification.schedule && localNotification.schedule.at) {
if (+localNotification.schedule.at <= now) {
this.buildNotification(localNotification);
toRemove.push(localNotification);
}
}
});
console.log('Sent pending, removing', toRemove);

this.pending = this.pending.filter(localNotification => !toRemove.find(ln => ln === localNotification));
}

sendNotification(localNotification: LocalNotification): Notification {
const l = localNotification;

if (localNotification.schedule && localNotification.schedule.at) {
const diff = +localNotification.schedule.at - +new Date;
this.pending.push(l);
setTimeout(() => {
this.sendPending();
}, diff);
return;
}

this.buildNotification(localNotification);
}

buildNotification(localNotification: LocalNotification) {
const l = localNotification;
return new Notification(l.title, {
body: l.body
});
}

schedule(options: { notifications: LocalNotification[]; }): Promise<LocalNotificationScheduleResult> {
const notifications: Notification[] = [];
options.notifications.forEach(notification => {
notifications.push(this.sendNotification(notification));
});

return Promise.resolve({
notifications: notifications.map(_ => { return { id: '' }})
});
}

getPending(): Promise<LocalNotificationPendingList> {
return Promise.resolve({
notifications: this.pending.map(localNotification => {
return {
id: '' + localNotification.id
}
})
});
}

registerActionTypes(_options: { types: LocalNotificationActionType[]; }): Promise<void> {
throw new Error("Method not implemented.");
}

cancel(pending: LocalNotificationPendingList): Promise<void> {
console.log('Cancel these', pending);
this.pending = this.pending.filter(
localNotification => !pending.notifications.find(ln => ln.id === '' + localNotification.id));
return Promise.resolve();
}

areEnabled(): Promise<LocalNotificationEnabledResult> {
throw new Error("Method not implemented.");
}


requestPermissions(): Promise<PermissionsRequestResult> {
return new Promise((resolve, reject) => {
Notification.requestPermission().then((result) => {
if(result === 'denied' || result === 'default') {
reject(result);
return;
}
resolve({
results: [ result ]
});
}).catch((e) => {
reject(e);
});
});
}
}

const LocalNotifications = new LocalNotificationsPluginWeb();

export { LocalNotifications };
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@


<ion-content padding>
<button (click)="init()" ion-button>
Init
</button>
<br/><br/>
<button (click)="scheduleNow()" ion-button>
Schedule Now
</button>
Expand Down
5 changes: 5 additions & 0 deletions example/src/pages/local-notifications/local-notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export class LocalNotificationsPage {
pendingNotifs: LocalNotificationScheduleResult;

constructor(public navCtrl: NavController, public navParams: NavParams) {
}

async init() {
await Plugins.LocalNotifications.requestPermissions();

try {
Plugins.LocalNotifications.registerActionTypes({
types: [
Expand Down

0 comments on commit 54d3a1b

Please sign in to comment.