Skip turn notifications when a Microchess tab is already visible.
@@ -163,7 +163,7 @@ The replay footer’s chain icon copies a permanent link with an unguessable tok
163
163
164
164
## Notifications and home-screen installation
165
165
166
**Account → Enable turn notifications** opts this browser/device in. Notifications cover a new turn (including accepted challenges, match links, matchmaking and rematches), incoming challenges and rematch requests. Enabling on another device adds that device; disabling affects only the current one. Sign-out removes the current browser's server registration. An existing browser subscription is associated with the current account after signing in or switching accounts. There is a limit of ten subscriptions per account.
166
**Account → Enable turn notifications** opts this browser/device in. Notifications cover a new turn (including accepted challenges, match links, matchmaking and rematches), incoming challenges and rematch requests. The service worker skips showing an OS notification when any Microchess tab in that browser is already visible; background/hidden tabs and other devices still notify. Enabling on another device adds that device; disabling affects only the current one. Sign-out removes the current browser's server registration. An existing browser subscription is associated with the current account after signing in or switching accounts. There is a limit of ten subscriptions per account.
167
167
168
168
Game actions collect notification events inside the game transaction and dispatch only after commit. There is no cronjob, persistent notification queue, polling-based notification scan or background worker. Matchmaking triggered by a state request also dispatches its newly created games. PHP closes the session before sending; with PHP-FPM it finishes the HTTP response first. Other SAPIs may wait for push delivery (two-second connection timeout, five-second request timeout). Delivery failures are logged without endpoints/keys and cannot roll back a move. Expired subscriptions are removed. Failed deliveries are not retried; notifications are best effort. Superseded game versions are skipped before sending, messages expire after one hour, and each game's topic/tag replaces older pending/displayed notifications.
169
169
@@ -163,7 +163,7 @@ The replay footer’s chain icon copies a permanent link with an unguessable tok163
163
164
## Notifications and home-screen installation164
## Notifications and home-screen installation165
165
166
**Account → Enable turn notifications** opts this browser/device in. Notifications cover a new turn (including accepted challenges, match links, matchmaking and rematches), incoming challenges and rematch requests. Enabling on another device adds that device; disabling affects only the current one. Sign-out removes the current browser's server registration. An existing browser subscription is associated with the current account after signing in or switching accounts. There is a limit of ten subscriptions per account.166
**Account → Enable turn notifications** opts this browser/device in. Notifications cover a new turn (including accepted challenges, match links, matchmaking and rematches), incoming challenges and rematch requests. The service worker skips showing an OS notification when any Microchess tab in that browser is already visible; background/hidden tabs and other devices still notify. Enabling on another device adds that device; disabling affects only the current one. Sign-out removes the current browser's server registration. An existing browser subscription is associated with the current account after signing in or switching accounts. There is a limit of ten subscriptions per account.167
167
168
Game actions collect notification events inside the game transaction and dispatch only after commit. There is no cronjob, persistent notification queue, polling-based notification scan or background worker. Matchmaking triggered by a state request also dispatches its newly created games. PHP closes the session before sending; with PHP-FPM it finishes the HTTP response first. Other SAPIs may wait for push delivery (two-second connection timeout, five-second request timeout). Delivery failures are logged without endpoints/keys and cannot roll back a move. Expired subscriptions are removed. Failed deliveries are not retried; notifications are best effort. Superseded game versions are skipped before sending, messages expire after one hour, and each game's topic/tag replaces older pending/displayed notifications.168
Game actions collect notification events inside the game transaction and dispatch only after commit. There is no cronjob, persistent notification queue, polling-based notification scan or background worker. Matchmaking triggered by a state request also dispatches its newly created games. PHP closes the session before sending; with PHP-FPM it finishes the HTTP response first. Other SAPIs may wait for push delivery (two-second connection timeout, five-second request timeout). Delivery failures are logged without endpoints/keys and cannot roll back a move. Expired subscriptions are removed. Failed deliveries are not retried; notifications are best effort. Superseded game versions are skipped before sending, messages expire after one hour, and each game's topic/tag replaces older pending/displayed notifications.169
169
@@ -7,12 +7,18 @@ self.addEventListener('push', event =>
7
7
let data = {};
8
8
try { data = event.data?.json() || {}; } catch (_) {}
9
9
const game = Number.isSafeInteger(data.game) && data.game > 0 ? data.game : null;
10
event.waitUntil(self.registration.showNotification('Microchess', {
11
body: typeof data.body === 'string' ? data.body : 'A game is waiting for you.',
12
icon: new URL('assets/app-icon-192.png', self.registration.scope).href,
13
tag: game ? `game-${game}` : 'microchess',
14
data: { game }
15
}));
10
event.waitUntil((async () =>
11
{
12
// Skip OS notifications while the player is already looking at Microchess.
13
const windows = await self.clients.matchAll({ type: 'window', includeUncontrolled: true });
14
if (windows.some(client => client.visibilityState === 'visible')) return;
15
await self.registration.showNotification('Microchess', {
16
body: typeof data.body === 'string' ? data.body : 'A game is waiting for you.',
17
icon: new URL('assets/app-icon-192.png', self.registration.scope).href,
18
tag: game ? `game-${game}` : 'microchess',
19
data: { game }
20
});
21
})());
16
22
});
17
23
self.addEventListener('notificationclick', event =>
18
24
{
@@ -7,12 +7,18 @@ self.addEventListener('push', event =>7
let data = {};7
let data = {};8
try { data = event.data?.json() || {}; } catch (_) {}8
try { data = event.data?.json() || {}; } catch (_) {}9
const game = Number.isSafeInteger(data.game) && data.game > 0 ? data.game : null;9
const game = Number.isSafeInteger(data.game) && data.game > 0 ? data.game : null;10
event.waitUntil(self.registration.showNotification('Microchess', {10
event.waitUntil((async () =>11
body: typeof data.body === 'string' ? data.body : 'A game is waiting for you.',11
{12
icon: new URL('assets/app-icon-192.png', self.registration.scope).href,12
// Skip OS notifications while the player is already looking at Microchess.13
tag: game ? `game-${game}` : 'microchess',13
const windows = await self.clients.matchAll({ type: 'window', includeUncontrolled: true });14
data: { game }14
if (windows.some(client => client.visibilityState === 'visible')) return;15
}));15
await self.registration.showNotification('Microchess', {16
body: typeof data.body === 'string' ? data.body : 'A game is waiting for you.',17
icon: new URL('assets/app-icon-192.png', self.registration.scope).href,18
tag: game ? `game-${game}` : 'microchess',19
data: { game }20
});21
})());16
});22
});17
self.addEventListener('notificationclick', event =>23
self.addEventListener('notificationclick', event =>18
{24
{@@ -21,6 +21,14 @@ await dispatch('push', { data: { json: () => ({ game: 42, body: 'Your turn again
21
21
assert.equal(shown[0].tag, 'game-42');
22
22
assert.equal(shown[0].icon, scope + 'assets/app-icon-192.png');
23
23
assert.equal(shown[0].data.game, 42);
24
windows = [{ url: scope, visibilityState: 'visible' }];
25
await dispatch('push', { data: { json: () => ({ game: 99, body: 'Should stay silent.' }) } });
26
assert.equal(shown.length, 1, 'Visible Microchess tab suppresses OS notification');
27
windows = [{ url: scope, visibilityState: 'hidden' }];
28
await dispatch('push', { data: { json: () => ({ game: 100, body: 'Background tab still notifies.' }) } });
29
assert.equal(shown.length, 2, 'Hidden tab still shows a notification');
30
assert.equal(shown[1].tag, 'game-100');
31
windows = [];
24
32
let closed = false;
25
33
const notification = { data: { game: 42 }, close: () => { closed = true; } };
26
34
await dispatch('notificationclick', { notification });
@@ -35,7 +43,7 @@ windows = [{ url: 'https://example.test/other-app/', navigate: async () => { thr
35
43
await dispatch('notificationclick', { notification });
36
44
assert.equal(opened.length, 2, 'Another application is not navigated');
37
45
await dispatch('push', { data: { json: () => { throw Error('Malformed payload'); } } });
38
assert.equal(shown[1].body, 'A game is waiting for you.');
46
assert.equal(shown[2].body, 'A game is waiting for you.');
39
47
await dispatch('notificationclick', { notification: { data: { game: 'https://evil.test/' }, close: () => {} } });
40
48
assert.equal(opened.at(-1), scope, 'Notification data cannot redirect outside app');
41
console.log('PWA: notification display, subdirectory links, window reuse and malformed payload checks passed.');
49
console.log('PWA: notification display, visible-tab suppression, subdirectory links, window reuse and malformed payload checks passed.');
@@ -21,6 +21,14 @@ await dispatch('push', { data: { json: () => ({ game: 42, body: 'Your turn again21
assert.equal(shown[0].tag, 'game-42');21
assert.equal(shown[0].tag, 'game-42');22
assert.equal(shown[0].icon, scope + 'assets/app-icon-192.png');22
assert.equal(shown[0].icon, scope + 'assets/app-icon-192.png');23
assert.equal(shown[0].data.game, 42);23
assert.equal(shown[0].data.game, 42);24
windows = [{ url: scope, visibilityState: 'visible' }];25
await dispatch('push', { data: { json: () => ({ game: 99, body: 'Should stay silent.' }) } });26
assert.equal(shown.length, 1, 'Visible Microchess tab suppresses OS notification');27
windows = [{ url: scope, visibilityState: 'hidden' }];28
await dispatch('push', { data: { json: () => ({ game: 100, body: 'Background tab still notifies.' }) } });29
assert.equal(shown.length, 2, 'Hidden tab still shows a notification');30
assert.equal(shown[1].tag, 'game-100');31
windows = [];24
let closed = false;32
let closed = false;25
const notification = { data: { game: 42 }, close: () => { closed = true; } };33
const notification = { data: { game: 42 }, close: () => { closed = true; } };26
await dispatch('notificationclick', { notification });34
await dispatch('notificationclick', { notification });@@ -35,7 +43,7 @@ windows = [{ url: 'https://example.test/other-app/', navigate: async () => { thr35
await dispatch('notificationclick', { notification });43
await dispatch('notificationclick', { notification });36
assert.equal(opened.length, 2, 'Another application is not navigated');44
assert.equal(opened.length, 2, 'Another application is not navigated');37
await dispatch('push', { data: { json: () => { throw Error('Malformed payload'); } } });45
await dispatch('push', { data: { json: () => { throw Error('Malformed payload'); } } });38
assert.equal(shown[1].body, 'A game is waiting for you.');46
assert.equal(shown[2].body, 'A game is waiting for you.');39
await dispatch('notificationclick', { notification: { data: { game: 'https://evil.test/' }, close: () => {} } });47
await dispatch('notificationclick', { notification: { data: { game: 'https://evil.test/' }, close: () => {} } });40
assert.equal(opened.at(-1), scope, 'Notification data cannot redirect outside app');48
assert.equal(opened.at(-1), scope, 'Notification data cannot redirect outside app');41
console.log('PWA: notification display, subdirectory links, window reuse and malformed payload checks passed.');49
console.log('PWA: notification display, visible-tab suppression, subdirectory links, window reuse and malformed payload checks passed.');