refactor(service-worker): Rework notification click handler (#25860)

- Add missing image and timestamp properties
- Remove focus from click handler

PR Close #25860
This commit is contained in:
Joost Zöllner 2018-09-28 09:47:14 +02:00 committed by Kara Erickson
parent c78c221028
commit c60418b1f4

View File

@ -34,8 +34,8 @@ const NOTIFICATION_OPTION_NAMES = [
'vibrate', 'data' 'vibrate', 'data'
]; ];
const NOTIFICATION_CLICK_OPTION_NAMES = [ const NOTIFICATION_CLICK_OPTION_NAMES = [
'actions', 'badge', 'title', 'body', 'dir', 'icon', 'lang', 'renotify', 'requireInteraction', 'actions', 'badge', 'title', 'body', 'dir', 'icon', 'image', 'lang', 'renotify',
'tag', 'vibrate', 'data' 'requireInteraction', 'tag', 'timestamp', 'vibrate', 'data'
]; ];
interface LatestEntry { interface LatestEntry {
@ -309,15 +309,15 @@ export class Driver implements Debuggable, UpdateSource {
await this.scope.registration.showNotification(desc['title'] !, options); await this.scope.registration.showNotification(desc['title'] !, options);
} }
private async handleClick(notification: any, action?: string): Promise<void> { private async handleClick(notification: Notification, action?: string): Promise<void> {
(notification as Notification).close(); notification.close();
const desc = notification as{[key: string]: string | undefined}; const desc = notification as any;
let options: {[key: string]: string | undefined} = {}; let options: {[key: string]: string | undefined} = {};
NOTIFICATION_CLICK_OPTION_NAMES.filter(name => desc.hasOwnProperty(name)) NOTIFICATION_CLICK_OPTION_NAMES.filter(name => desc.hasOwnProperty(name))
.forEach(name => options[name] = desc[name]); .forEach(name => options[name] = desc[name]);
await this.broadcastAndFocus({ await this.broadcast({
type: 'NOTIFICATION_CLICK', type: 'NOTIFICATION_CLICK',
data: {action, notification: options}, data: {action, notification: options},
}); });
@ -1007,18 +1007,6 @@ export class Driver implements Debuggable, UpdateSource {
}, Promise.resolve()); }, Promise.resolve());
} }
async broadcastAndFocus(msg: Object): Promise<void> {
const clients = await this.scope.clients.matchAll();
clients.forEach((client: any) => {
if ('focus' in client) {
if (!client.focused) {
client.focus();
}
}
client.postMessage(msg);
});
}
async broadcast(msg: Object): Promise<void> { async broadcast(msg: Object): Promise<void> {
const clients = await this.scope.clients.matchAll(); const clients = await this.scope.clients.matchAll();
clients.forEach(client => { client.postMessage(msg); }); clients.forEach(client => { client.postMessage(msg); });