@ultimat3/notify 31.49.0 → 31.51.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/package.json +8 -8
- package/src/apns.ts +8 -1
- package/src/channel-push.ts +15 -0
- package/src/errors.ts +3 -3
- package/src/fcm.ts +6 -0
- package/src/index.ts +1 -1
- package/src/native-push.ts +74 -10
- package/src/push-errors.ts +33 -5
- package/src/push-message.ts +56 -1
package/README.md
CHANGED
|
@@ -309,10 +309,12 @@ export const approvalRequested = notifier({
|
|
|
309
309
|
| registration | `pushRegister({ permission, platforms, devices })`; `previousToken` forgets the token the OS rotated away. `pushUnregister(…)` on sign-out — a token registered by a second person on the same phone is theirs now. Past `PUSH_DEVICES_PER_ACTOR` (20) the caller's oldest registration is evicted. An agent never registers. An iOS token is hex (it is spliced into the APNs path), an Android one FCM's alphabet — anything else is a 422 |
|
|
310
310
|
| data-only | `pushChannel({ pusher, data })`: APNs `background` at priority 5 with `content-available: 1`, FCM with no `notification` block. At most 8 string keys, 256 characters each, no provider-reserved key (`aps`, `notification`, `google.*`…) — else `X_NOTIFY_PUSH_MESSAGE_INVALID` before a request. `webPush()` has no `sendData`, so handing it a `data` channel is a type error |
|
|
311
311
|
| visible | `pushChannel({ pusher, message })` as for the web: title and body rendered per device locale; `url` travels as data; `topic ?? tag` is the collapse id; `urgency` low/very-low sends at the lower priority. `icon`, `badge`, `actions`, `requireInteraction`, `renotify` have no native meaning and are not sent |
|
|
312
|
+
| native-only | `sound` (both), `interruptionLevel` and `category` (APNs `aps`), `channelId` (FCM `android.notification`) on the same message; `webPush()` ignores them. `sound`: `'default'`, or a bundled sound by its APNs file name / FCM `res/raw` name (no extension); unset, APNs alerts silently and Android 8+ plays the channel's sound. Names of 1–64 `A-Za-z0-9._-`, any position, else `X_NOTIFY_PUSH_MESSAGE_INVALID` with `meta.field` before a request. Unset, the wire is unchanged |
|
|
313
|
+
| report | `nativePush({ onReport })` gets `{ to, kind, report }` per settled send — `NativePushReport { accepted, refused, removed, skipped }`, per device. `accepted` = the provider took it, never "delivered" |
|
|
312
314
|
|
|
313
315
|
| Provider answer | What happens |
|
|
314
316
|
|---|---|
|
|
315
|
-
| 2xx |
|
|
317
|
+
| 2xx | accepted by the provider (`report.accepted`) |
|
|
316
318
|
| APNs 410, FCM `UNREGISTERED` | `X_NOTIFY_PUSH_DEVICE_GONE`: the row is deleted, logged, and the delivery still settles. A bare FCM 404 is **not** gone (a wrong `projectId` is a 404 too) |
|
|
317
319
|
| APNs 400 `BadDeviceToken`, FCM 400 `INVALID_ARGUMENT` on `message.token` | `X_NOTIFY_PUSH_PROVIDER_REFUSED` with `meta.badToken`; counted on the row, which is deleted on the `PUSH_BAD_TOKEN_LIMIT`th (3) answer in a row — never the first (`BadDeviceToken` is also a sandbox token sent to production). A delivery resets the count. An `INVALID_ARGUMENT` on the payload never counts |
|
|
318
320
|
| 401/403, or Google refusing the assertion | `X_NOTIFY_PUSH_PROVIDER_REFUSED` with `meta.credential`: that provider's fan-out stops on its first device, the cached credential the refused request carried is dropped (only that one — a straggler's late 403 keeps a fresh token), and the refusal fails the delivery as `terminal` |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/notify",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.51.0",
|
|
4
4
|
"description": "Notifications: one declaration, many channels — fan-out, preference gate, digest window, delivery ledger, in-app inbox",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -46,12 +46,12 @@
|
|
|
46
46
|
"test": "bun test"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@ultimat3/action": "31.
|
|
50
|
-
"@ultimat3/core": "31.
|
|
51
|
-
"@ultimat3/i18n": "31.
|
|
52
|
-
"@ultimat3/jobs": "31.
|
|
53
|
-
"@ultimat3/policy": "31.
|
|
54
|
-
"@ultimat3/schema": "31.
|
|
55
|
-
"@ultimat3/time": "31.
|
|
49
|
+
"@ultimat3/action": "31.51.0",
|
|
50
|
+
"@ultimat3/core": "31.51.0",
|
|
51
|
+
"@ultimat3/i18n": "31.51.0",
|
|
52
|
+
"@ultimat3/jobs": "31.51.0",
|
|
53
|
+
"@ultimat3/policy": "31.51.0",
|
|
54
|
+
"@ultimat3/schema": "31.51.0",
|
|
55
|
+
"@ultimat3/time": "31.51.0"
|
|
56
56
|
}
|
|
57
57
|
}
|
package/src/apns.ts
CHANGED
|
@@ -79,7 +79,14 @@ export function apnsRequest(
|
|
|
79
79
|
return {
|
|
80
80
|
headers,
|
|
81
81
|
body: JSON.stringify({
|
|
82
|
-
aps: {
|
|
82
|
+
aps: {
|
|
83
|
+
alert: { title: alert.title, body: alert.body },
|
|
84
|
+
...(alert.sound === undefined ? {} : { sound: alert.sound }),
|
|
85
|
+
...(alert.category === undefined ? {} : { category: alert.category }),
|
|
86
|
+
...(alert.interruptionLevel === undefined
|
|
87
|
+
? {}
|
|
88
|
+
: { 'interruption-level': alert.interruptionLevel }),
|
|
89
|
+
},
|
|
83
90
|
url: alert.url,
|
|
84
91
|
}),
|
|
85
92
|
};
|
package/src/channel-push.ts
CHANGED
|
@@ -48,6 +48,21 @@ export interface NotifyPushMessage {
|
|
|
48
48
|
readonly urgency?: 'very-low' | 'low' | 'normal' | 'high' | undefined;
|
|
49
49
|
/** Collapse key AT THE PUSH SERVICE, for a device that is offline. */
|
|
50
50
|
readonly topic?: string | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* NATIVE ONLY — a browser push has no such field and `webPush()` sends none of these. The sound
|
|
53
|
+
* an iOS or Android device plays: `'default'` for the platform's own, or one the app bundles —
|
|
54
|
+
* APNs reads its file name (`chime.caf`), FCM its `res/raw` resource name without the extension
|
|
55
|
+
* (`chime`), so one string names a bundled sound on both only when the two agree. On Android 8
|
|
56
|
+
* and later the notification channel sets the sound. Unset, none is requested: APNs shows the
|
|
57
|
+
* alert silently.
|
|
58
|
+
*/
|
|
59
|
+
readonly sound?: string | undefined;
|
|
60
|
+
/** iOS: how far the alert may break through Focus. `critical` needs Apple's entitlement. */
|
|
61
|
+
readonly interruptionLevel?: 'passive' | 'active' | 'time-sensitive' | 'critical' | undefined;
|
|
62
|
+
/** iOS: the notification category the app registered, which names its action buttons. */
|
|
63
|
+
readonly category?: string | undefined;
|
|
64
|
+
/** Android: the notification channel the app created, which sets importance and sound. */
|
|
65
|
+
readonly channelId?: string | undefined;
|
|
51
66
|
}
|
|
52
67
|
|
|
53
68
|
/** The seam. `@ultimat3/pwa`'s `webPush()` is the framework's; a vendor SDK fits it too. */
|
package/src/errors.ts
CHANGED
|
@@ -38,7 +38,7 @@ export const NOTIFY_ERROR_TITLES: Readonly<Record<NotifyErrorCode, string>> = {
|
|
|
38
38
|
X_NOTIFY_DELIVERY_HELD: 'another attempt holds this delivery',
|
|
39
39
|
X_NOTIFY_PUSH_DEVICE_GONE: 'the push provider says a device token is dead',
|
|
40
40
|
X_NOTIFY_PUSH_PROVIDER_REFUSED: 'APNs or FCM refused a push for good',
|
|
41
|
-
X_NOTIFY_PUSH_MESSAGE_INVALID: 'a
|
|
41
|
+
X_NOTIFY_PUSH_MESSAGE_INVALID: 'a native push carries what its provider must not or cannot take',
|
|
42
42
|
X_NOTIFY_SMS_TRANSPORT_MISSING: 'an SMS was sent with no SmsTransport installed',
|
|
43
43
|
X_NOTIFY_SMS_NUMBER_INVALID: 'an SMS number is not E.164',
|
|
44
44
|
};
|
|
@@ -83,8 +83,8 @@ registerErrorRetry({
|
|
|
83
83
|
// Waited out with the attempt uncounted and `maxDelay` no cap (`rateLimitDeferralMs`, jobs).
|
|
84
84
|
X_NOTIFY_DELIVERY_HELD: 'retry-after',
|
|
85
85
|
// Native push (`push-errors.ts`). A refusal the provider gives again — a bad credential, a wrong
|
|
86
|
-
// topic, a payload over its limit — and a data push carrying more than ids
|
|
87
|
-
// either. `X_NOTIFY_PUSH_DEVICE_GONE` is absent: `nativePush()` catches it, deletes the row, and
|
|
86
|
+
// topic, a payload over its limit — and a data push carrying more than ids, or a sound, category,
|
|
87
|
+
// channel or interruption level no provider takes: no attempt fixes either. `X_NOTIFY_PUSH_DEVICE_GONE` is absent: `nativePush()` catches it, deletes the row, and
|
|
88
88
|
// it never leaves a send.
|
|
89
89
|
X_NOTIFY_PUSH_PROVIDER_REFUSED: 'terminal',
|
|
90
90
|
X_NOTIFY_PUSH_MESSAGE_INVALID: 'terminal',
|
package/src/fcm.ts
CHANGED
|
@@ -59,6 +59,11 @@ export function fcmMessage(token: string, push: NativePush): Record<string, unkn
|
|
|
59
59
|
return { token, data: { ...push.data }, android: { priority: 'high' } };
|
|
60
60
|
}
|
|
61
61
|
const { push: alert } = push;
|
|
62
|
+
// Android's own block only when it says something: an alert without these renders as before.
|
|
63
|
+
const shown = {
|
|
64
|
+
...(alert.sound === undefined ? {} : { sound: alert.sound }),
|
|
65
|
+
...(alert.channelId === undefined ? {} : { channel_id: alert.channelId }),
|
|
66
|
+
};
|
|
62
67
|
return {
|
|
63
68
|
token,
|
|
64
69
|
notification: { title: alert.title, body: alert.body },
|
|
@@ -67,6 +72,7 @@ export function fcmMessage(token: string, push: NativePush): Record<string, unkn
|
|
|
67
72
|
priority: alert.urgent ? 'high' : 'normal',
|
|
68
73
|
ttl: `${String(alert.ttlSeconds)}s`,
|
|
69
74
|
...(alert.collapse === undefined ? {} : { collapse_key: alert.collapse }),
|
|
75
|
+
...(Object.keys(shown).length === 0 ? {} : { notification: shown }),
|
|
70
76
|
},
|
|
71
77
|
};
|
|
72
78
|
}
|
package/src/index.ts
CHANGED
|
@@ -80,7 +80,7 @@ export {
|
|
|
80
80
|
postgresDeliveryLedger,
|
|
81
81
|
SQL_NOTIFY_DELIVERIES_TABLE,
|
|
82
82
|
} from './ledger-pg';
|
|
83
|
-
export type { NativePusher, NativePushOptions } from './native-push';
|
|
83
|
+
export type { NativePusher, NativePushOptions, NativePushReport } from './native-push';
|
|
84
84
|
export {
|
|
85
85
|
nativePush,
|
|
86
86
|
PUSH_BAD_TOKEN_LIMIT,
|
package/src/native-push.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// one, which `webPush()` cannot (a browser push always shows) — so that mistake is a type error.
|
|
5
5
|
|
|
6
6
|
import type { Clock } from '@ultimat3/core';
|
|
7
|
-
import { finiteCount, isUltimateError, logger, systemClock } from '@ultimat3/core';
|
|
7
|
+
import { finiteCount, isUltimateError, logger, reportError, systemClock } from '@ultimat3/core';
|
|
8
8
|
import type { OutboundFetch } from '@ultimat3/core/outbound';
|
|
9
9
|
import { translatorFor } from '@ultimat3/i18n';
|
|
10
10
|
import type { ApnsOptions } from './apns';
|
|
@@ -16,7 +16,7 @@ import { platformFetch } from './native-transport';
|
|
|
16
16
|
import type { PushDeviceStore, PushPlatform } from './push-device';
|
|
17
17
|
import { isDeviceToken } from './push-device';
|
|
18
18
|
import type { NativePush, NativeSender, PushData, PushTranslate } from './push-message';
|
|
19
|
-
import { renderNativePush, screenPushData } from './push-message';
|
|
19
|
+
import { renderNativePush, screenNativeFields, screenPushData } from './push-message';
|
|
20
20
|
|
|
21
21
|
export const DEFAULT_NATIVE_PUSH_DEADLINE_MS = 10_000;
|
|
22
22
|
|
|
@@ -41,9 +41,41 @@ export interface NativePushOptions {
|
|
|
41
41
|
readonly env?: Readonly<Record<string, string | undefined>> | undefined;
|
|
42
42
|
readonly clock?: Clock | undefined;
|
|
43
43
|
readonly translate?: ((locale: string) => PushTranslate) | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* Each settled send's report, once every device was tried — `kind` names `send` (`'alert'`) or
|
|
46
|
+
* `sendData` (`'data'`). A send that rejects reports nothing. A throw here is reported and the
|
|
47
|
+
* send still settles: the providers already took it, and a retry would push it twice.
|
|
48
|
+
*/
|
|
49
|
+
readonly onReport?:
|
|
50
|
+
| ((sent: {
|
|
51
|
+
readonly to: string;
|
|
52
|
+
readonly kind: 'alert' | 'data';
|
|
53
|
+
readonly report: NativePushReport;
|
|
54
|
+
}) => void)
|
|
55
|
+
| undefined;
|
|
44
56
|
}
|
|
45
57
|
|
|
46
|
-
/**
|
|
58
|
+
/**
|
|
59
|
+
* What one send did with the recipient's devices. `accepted` is what the PROVIDER took (a 2xx from
|
|
60
|
+
* APNs or FCM) — never what a device received or showed: neither provider reports that.
|
|
61
|
+
*/
|
|
62
|
+
export interface NativePushReport {
|
|
63
|
+
readonly accepted: number;
|
|
64
|
+
/** Refused by the provider and kept: a payload it will not take, a token not yet at its limit. */
|
|
65
|
+
readonly refused: number;
|
|
66
|
+
/** Rows deleted: gone (APNs 410, FCM `UNREGISTERED`), a bad token at its limit, or unsendable. */
|
|
67
|
+
readonly removed: number;
|
|
68
|
+
/**
|
|
69
|
+
* Never sent: its platform has no provider configured. A credential refusal rejects the whole
|
|
70
|
+
* send instead, so no report carries the devices it stopped.
|
|
71
|
+
*/
|
|
72
|
+
readonly skipped: number;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Both seams, each settling once every device was tried (its report to `onReport`). A send nothing
|
|
77
|
+
* accepted and something refused still rejects with the first refusal, as a delivery must.
|
|
78
|
+
*/
|
|
47
79
|
export interface NativePusher extends Pusher, DataPusher {
|
|
48
80
|
send(push: Parameters<Pusher['send']>[0]): Promise<void>;
|
|
49
81
|
sendData(push: Parameters<DataPusher['sendData']>[0]): Promise<void>;
|
|
@@ -70,16 +102,23 @@ async function eachDevice(
|
|
|
70
102
|
devices: PushDeviceStore,
|
|
71
103
|
senders: Senders,
|
|
72
104
|
push: { readonly to: string; readonly signal: AbortSignal; readonly message: PushFor },
|
|
73
|
-
): Promise<
|
|
105
|
+
): Promise<NativePushReport> {
|
|
74
106
|
let delivered = 0;
|
|
107
|
+
let refused = 0;
|
|
108
|
+
let removed = 0;
|
|
109
|
+
let skipped = 0;
|
|
75
110
|
let retry: unknown;
|
|
76
111
|
let refusal: unknown;
|
|
77
112
|
let credential: unknown;
|
|
78
113
|
const stopped = new Set<PushPlatform>();
|
|
79
114
|
for (const device of await devices.listFor(push.to)) {
|
|
80
|
-
if (stopped.has(device.platform))
|
|
115
|
+
if (stopped.has(device.platform)) {
|
|
116
|
+
skipped += 1;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
81
119
|
const sender = senders.get(device.platform);
|
|
82
120
|
if (sender === undefined) {
|
|
121
|
+
skipped += 1;
|
|
83
122
|
logger.warn('notify.push.unconfigured', {
|
|
84
123
|
platform: device.platform,
|
|
85
124
|
fix: `nativePush({ ${device.platform === 'ios' ? 'apns' : 'fcm'}: { … } }) # or drop '${device.platform}' from pushRegister({ platforms })`,
|
|
@@ -89,6 +128,7 @@ async function eachDevice(
|
|
|
89
128
|
// A stored token no provider could take is a row no send will ever reach.
|
|
90
129
|
if (!isDeviceToken(device.platform, device.token)) {
|
|
91
130
|
await devices.remove(device.platform, device.token);
|
|
131
|
+
removed += 1;
|
|
92
132
|
continue;
|
|
93
133
|
}
|
|
94
134
|
try {
|
|
@@ -102,6 +142,7 @@ async function eachDevice(
|
|
|
102
142
|
}
|
|
103
143
|
if (error.code === 'X_NOTIFY_PUSH_DEVICE_GONE') {
|
|
104
144
|
await devices.remove(device.platform, device.token);
|
|
145
|
+
removed += 1;
|
|
105
146
|
// info: a rare outcome — the provider retired this device and its row was deleted; the only record of why.
|
|
106
147
|
logger.info('notify.push.gone', { code: error.code, cause: error.cause });
|
|
107
148
|
continue;
|
|
@@ -111,6 +152,7 @@ async function eachDevice(
|
|
|
111
152
|
if (meta['credential'] === true) {
|
|
112
153
|
stopped.add(device.platform);
|
|
113
154
|
credential ??= error;
|
|
155
|
+
refused += 1;
|
|
114
156
|
continue;
|
|
115
157
|
}
|
|
116
158
|
refusal ??= error;
|
|
@@ -119,14 +161,16 @@ async function eachDevice(
|
|
|
119
161
|
(await devices.noteBadToken(device.platform, device.token)) >= PUSH_BAD_TOKEN_LIMIT
|
|
120
162
|
) {
|
|
121
163
|
await devices.remove(device.platform, device.token);
|
|
164
|
+
removed += 1;
|
|
122
165
|
// info: a rare outcome — a token refused PUSH_BAD_TOKEN_LIMIT times was deleted; the only record of why.
|
|
123
166
|
logger.info('notify.push.bad_token_removed', { code: error.code, cause: error.cause });
|
|
124
|
-
}
|
|
167
|
+
} else refused += 1;
|
|
125
168
|
}
|
|
126
169
|
}
|
|
127
170
|
if (credential !== undefined) throw credential;
|
|
128
171
|
if (retry !== undefined) throw retry;
|
|
129
172
|
if (delivered === 0 && refusal !== undefined) throw refusal;
|
|
173
|
+
return { accepted: delivered, refused, removed, skipped };
|
|
130
174
|
}
|
|
131
175
|
|
|
132
176
|
/**
|
|
@@ -152,9 +196,27 @@ export function nativePush(options: NativePushOptions): NativePusher {
|
|
|
152
196
|
if (options.apns !== undefined) senders.set('ios', apnsSender(options.apns, deps));
|
|
153
197
|
if (options.fcm !== undefined) senders.set('android', fcmSender(options.fcm, deps));
|
|
154
198
|
const translate = options.translate ?? ((locale: string) => translatorFor(locale));
|
|
199
|
+
const settle = (to: string, kind: 'alert' | 'data', report: NativePushReport): void => {
|
|
200
|
+
if (options.onReport === undefined) return;
|
|
201
|
+
try {
|
|
202
|
+
options.onReport({ to, kind, report });
|
|
203
|
+
} catch (error) {
|
|
204
|
+
reportError(error, { source: 'job', scope: { operation: 'notify.push.on_report' } });
|
|
205
|
+
}
|
|
206
|
+
};
|
|
155
207
|
return {
|
|
156
|
-
send({
|
|
157
|
-
|
|
208
|
+
async send({
|
|
209
|
+
to,
|
|
210
|
+
message,
|
|
211
|
+
signal,
|
|
212
|
+
}: {
|
|
213
|
+
to: string;
|
|
214
|
+
message: NotifyPushMessage;
|
|
215
|
+
signal: AbortSignal;
|
|
216
|
+
}) {
|
|
217
|
+
// Screened once, before any device, as `sendData` screens its ids.
|
|
218
|
+
screenNativeFields(message);
|
|
219
|
+
const report = await eachDevice(options.devices, senders, {
|
|
158
220
|
to,
|
|
159
221
|
signal,
|
|
160
222
|
message: (locale) => ({
|
|
@@ -162,15 +224,17 @@ export function nativePush(options: NativePushOptions): NativePusher {
|
|
|
162
224
|
push: renderNativePush(message, translate(locale)),
|
|
163
225
|
}),
|
|
164
226
|
});
|
|
227
|
+
settle(to, 'alert', report);
|
|
165
228
|
},
|
|
166
|
-
sendData({ to, data, signal }: { to: string; data: PushData; signal: AbortSignal }) {
|
|
229
|
+
async sendData({ to, data, signal }: { to: string; data: PushData; signal: AbortSignal }) {
|
|
167
230
|
// Screened once, before any device: a refusal here is the notifier's, not a provider's.
|
|
168
231
|
const screened = screenPushData(data);
|
|
169
|
-
|
|
232
|
+
const report = await eachDevice(options.devices, senders, {
|
|
170
233
|
to,
|
|
171
234
|
signal,
|
|
172
235
|
message: () => ({ kind: 'data', data: screened }),
|
|
173
236
|
});
|
|
237
|
+
settle(to, 'data', report);
|
|
174
238
|
},
|
|
175
239
|
};
|
|
176
240
|
}
|
package/src/push-errors.ts
CHANGED
|
@@ -70,14 +70,42 @@ export class NotifyPushProviderRefusedError extends UltimateError {
|
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
|
|
73
|
+
const DATA_FIX =
|
|
74
|
+
'pushChannel({ pusher, data: ({ event }) => ({ id: event.params.id }) }) # in the notifier: one id per push, the app fetches the rest over its API';
|
|
75
|
+
|
|
76
|
+
/** Each a valid edit of the notifier's own `message`: the field set, everything else kept. */
|
|
77
|
+
const FIELD_FIX = {
|
|
78
|
+
sound:
|
|
79
|
+
"pushChannel({ pusher, message: (args) => ({ ...message(args), sound: 'default' }) }) # message: the notifier's current message function, its own keys and url kept; or a sound the app bundles: APNs takes its file name, FCM its res/raw name without the extension; 1 to 64 of A-Za-z0-9._-",
|
|
80
|
+
category:
|
|
81
|
+
"pushChannel({ pusher, message: (args) => ({ ...message(args), category: 'reply' }) }) # message: the notifier's current message function, its own keys and url kept; a category the iOS app registered; 1 to 64 of A-Za-z0-9._-",
|
|
82
|
+
channelId:
|
|
83
|
+
"pushChannel({ pusher, message: (args) => ({ ...message(args), channelId: 'mentions' }) }) # message: the notifier's current message function, its own keys and url kept; a channel the Android app created; 1 to 64 of A-Za-z0-9._-",
|
|
84
|
+
interruptionLevel:
|
|
85
|
+
"pushChannel({ pusher, message: (args) => ({ ...message(args), interruptionLevel: 'time-sensitive' }) }) # message: the notifier's current message function, its own keys and url kept; one of passive, active, time-sensitive, critical: fixed by APNs, critical needs Apple's entitlement",
|
|
86
|
+
} as const;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* A data-only push carrying more than an id: a reserved key, too many keys, a long value. Or, with
|
|
90
|
+
* `field`, a visible native push whose sound, category, channel or interruption level no provider
|
|
91
|
+
* would take.
|
|
92
|
+
*/
|
|
74
93
|
export class NotifyPushMessageInvalidError extends UltimateError {
|
|
75
|
-
constructor(input: {
|
|
94
|
+
constructor(input: {
|
|
95
|
+
problem: string;
|
|
96
|
+
field?: 'sound' | 'category' | 'channelId' | 'interruptionLevel' | undefined;
|
|
97
|
+
}) {
|
|
98
|
+
const { field } = input;
|
|
76
99
|
super({
|
|
77
100
|
code: 'X_NOTIFY_PUSH_MESSAGE_INVALID',
|
|
78
|
-
cause:
|
|
79
|
-
|
|
80
|
-
|
|
101
|
+
cause:
|
|
102
|
+
field === undefined
|
|
103
|
+
? `the data of a data-only push ${input.problem}; it travels through the provider, so it carries ids and nothing a person would read`
|
|
104
|
+
: field === 'interruptionLevel'
|
|
105
|
+
? `the interruptionLevel of a native push ${input.problem}; APNs takes only these fixed values`
|
|
106
|
+
: `the ${field} of a native push ${input.problem}; the provider takes it as a name the app registered or bundles`,
|
|
107
|
+
fix: field === undefined ? DATA_FIX : FIELD_FIX[field],
|
|
108
|
+
meta: field === undefined ? { problem: input.problem } : { problem: input.problem, field },
|
|
81
109
|
});
|
|
82
110
|
}
|
|
83
111
|
}
|
package/src/push-message.ts
CHANGED
|
@@ -72,11 +72,59 @@ export interface RenderedNativePush {
|
|
|
72
72
|
readonly collapse: string | undefined;
|
|
73
73
|
readonly ttlSeconds: number;
|
|
74
74
|
readonly urgent: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Both providers. Absent, no sound is requested: APNs shows the alert silently, and on Android 8
|
|
77
|
+
* and later the notification channel's sound plays. `'default'` asks for the platform's sound.
|
|
78
|
+
*/
|
|
79
|
+
readonly sound?: string | undefined;
|
|
80
|
+
/** APNs only. */
|
|
81
|
+
readonly interruptionLevel?: NativeInterruptionLevel | undefined;
|
|
82
|
+
/** APNs only. */
|
|
83
|
+
readonly category?: string | undefined;
|
|
84
|
+
/** FCM only. */
|
|
85
|
+
readonly channelId?: string | undefined;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export type NativeInterruptionLevel = 'passive' | 'active' | 'time-sensitive' | 'critical';
|
|
89
|
+
|
|
90
|
+
const INTERRUPTION_LEVELS: ReadonlySet<string> = new Set([
|
|
91
|
+
'passive',
|
|
92
|
+
'active',
|
|
93
|
+
'time-sensitive',
|
|
94
|
+
'critical',
|
|
95
|
+
]);
|
|
96
|
+
|
|
97
|
+
/** A sound file, a category or a channel id: a short name, never text a person reads. */
|
|
98
|
+
const NATIVE_NAME = /^[A-Za-z0-9._-]{1,64}$/;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The native-only fields of a visible push, screened once before any device: a name each provider
|
|
102
|
+
* takes as it is, or `X_NOTIFY_PUSH_MESSAGE_INVALID` naming the field.
|
|
103
|
+
*/
|
|
104
|
+
export function screenNativeFields(message: NotifyPushMessage): void {
|
|
105
|
+
for (const field of ['sound', 'category', 'channelId'] as const) {
|
|
106
|
+
const value: unknown = message[field];
|
|
107
|
+
if (value !== undefined && (typeof value !== 'string' || !NATIVE_NAME.test(value))) {
|
|
108
|
+
throw new NotifyPushMessageInvalidError({
|
|
109
|
+
field,
|
|
110
|
+
problem: `is not a name of 1 to 64 letters, digits, '.', '_' or '-' (${String(NATIVE_NAME)})`,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const level: unknown = message.interruptionLevel;
|
|
115
|
+
if (level !== undefined && (typeof level !== 'string' || !INTERRUPTION_LEVELS.has(level))) {
|
|
116
|
+
throw new NotifyPushMessageInvalidError({
|
|
117
|
+
field: 'interruptionLevel',
|
|
118
|
+
problem: `is not one of ${[...INTERRUPTION_LEVELS].join(', ')}`,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
75
121
|
}
|
|
76
122
|
|
|
77
123
|
/**
|
|
78
124
|
* The web-only fields (`icon`, `badge`, `actions`, `requireInteraction`, `renotify`) have no
|
|
79
|
-
* native meaning here and are not sent: an app's native shell decides how its alerts look.
|
|
125
|
+
* native meaning here and are not sent: an app's native shell decides how its alerts look. The
|
|
126
|
+
* native-only ones (`sound`, `interruptionLevel`, `category`, `channelId`) pass through as given,
|
|
127
|
+
* screened by `screenNativeFields`.
|
|
80
128
|
*/
|
|
81
129
|
export function renderNativePush(
|
|
82
130
|
message: NotifyPushMessage,
|
|
@@ -95,6 +143,13 @@ export function renderNativePush(
|
|
|
95
143
|
),
|
|
96
144
|
urgent:
|
|
97
145
|
message.urgency === undefined || message.urgency === 'normal' || message.urgency === 'high',
|
|
146
|
+
// Only the fields set: an alert without them renders exactly as it did before they existed.
|
|
147
|
+
...(message.sound === undefined ? {} : { sound: message.sound }),
|
|
148
|
+
...(message.interruptionLevel === undefined
|
|
149
|
+
? {}
|
|
150
|
+
: { interruptionLevel: message.interruptionLevel }),
|
|
151
|
+
...(message.category === undefined ? {} : { category: message.category }),
|
|
152
|
+
...(message.channelId === undefined ? {} : { channelId: message.channelId }),
|
|
98
153
|
};
|
|
99
154
|
}
|
|
100
155
|
|