@molecule/app-push 1.0.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/LICENSE +115 -0
- package/README.md +612 -0
- package/dist/index.d.ts +61 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +61 -0
- package/dist/index.js.map +1 -0
- package/dist/provider.d.ts +72 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +83 -0
- package/dist/provider.js.map +1 -0
- package/dist/types.d.ts +286 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/dist/web-provider.d.ts +13 -0
- package/dist/web-provider.d.ts.map +1 -0
- package/dist/web-provider.js +264 -0
- package/dist/web-provider.js.map +1 -0
- package/package.json +51 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-side push notifications interface for molecule.dev.
|
|
3
|
+
*
|
|
4
|
+
* Provides a unified API for push notifications across platforms (web,
|
|
5
|
+
* native containers, etc.): permission flow (`checkPermission`,
|
|
6
|
+
* `requestPermission`), registration (`register`, `getToken`), incoming
|
|
7
|
+
* events (`onNotificationReceived`, `onNotificationAction`), local
|
|
8
|
+
* notifications (`scheduleLocal`), and badges (`setBadge`, `clearBadge`).
|
|
9
|
+
* The device token is what your API's `@molecule/api-push-notifications`
|
|
10
|
+
* bond sends to.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import {
|
|
15
|
+
* getToken,
|
|
16
|
+
* onNotificationAction,
|
|
17
|
+
* register,
|
|
18
|
+
* requestPermission,
|
|
19
|
+
* } from '@molecule/app-push'
|
|
20
|
+
*
|
|
21
|
+
* async function enablePush(vapidPublicKey: string): Promise<string | null> {
|
|
22
|
+
* const permission = await requestPermission() // from a user gesture
|
|
23
|
+
* if (permission !== 'granted') return null
|
|
24
|
+
* const token = await register({ vapidPublicKey })
|
|
25
|
+
* // POST token.value to your API so api-push-notifications can target it
|
|
26
|
+
* return token.value
|
|
27
|
+
* }
|
|
28
|
+
*
|
|
29
|
+
* function handleTaps(open: (data: unknown) => void): () => void {
|
|
30
|
+
* return onNotificationAction((event) => open(event.notification.data))
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* - **No wiring is needed on web**: the first accessor call silently bonds
|
|
36
|
+
* the built-in Web Push provider. In a native app wire
|
|
37
|
+
* `@molecule/app-push-react-native` (the one prebuilt bond) via
|
|
38
|
+
* `setProvider()` BEFORE any push call — otherwise the web fallback gets
|
|
39
|
+
* bonded and native registration never happens.
|
|
40
|
+
* - **Web `register()` has three hard prerequisites**: (1) HTTPS (secure
|
|
41
|
+
* context), (2) a registered SERVICE WORKER — dev builds typically don't
|
|
42
|
+
* ship one, so register() throws "requires the production app build"
|
|
43
|
+
* (it fails fast instead of hanging), and (3) a VAPID public key
|
|
44
|
+
* (pass via `register({ vapidPublicKey })`) matching your API's
|
|
45
|
+
* `VAPID_PRIVATE_KEY` — without it Chrome may register but delivery
|
|
46
|
+
* fails.
|
|
47
|
+
* - **Request permission from a user gesture at the point of value**, never
|
|
48
|
+
* on load — a denied browser prompt is remembered forever (no re-prompt).
|
|
49
|
+
* - Web `scheduleLocal` at a future time uses an in-page timer: it does NOT
|
|
50
|
+
* survive reload/close. Web `cancelLocal`/`cancelAllLocal` are no-ops,
|
|
51
|
+
* and `getPendingLocal()` is always empty — for reliable scheduled
|
|
52
|
+
* delivery send a real push from the server.
|
|
53
|
+
* - `token.value` on web is a JSON-serialized PushSubscription (not an FCM
|
|
54
|
+
* token) — store it opaquely; platform is in `token.platform`.
|
|
55
|
+
*
|
|
56
|
+
* @module
|
|
57
|
+
*/
|
|
58
|
+
export * from './provider.js';
|
|
59
|
+
export * from './types.js';
|
|
60
|
+
export * from './web-provider.js';
|
|
61
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AAEH,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,mBAAmB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-side push notifications interface for molecule.dev.
|
|
3
|
+
*
|
|
4
|
+
* Provides a unified API for push notifications across platforms (web,
|
|
5
|
+
* native containers, etc.): permission flow (`checkPermission`,
|
|
6
|
+
* `requestPermission`), registration (`register`, `getToken`), incoming
|
|
7
|
+
* events (`onNotificationReceived`, `onNotificationAction`), local
|
|
8
|
+
* notifications (`scheduleLocal`), and badges (`setBadge`, `clearBadge`).
|
|
9
|
+
* The device token is what your API's `@molecule/api-push-notifications`
|
|
10
|
+
* bond sends to.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import {
|
|
15
|
+
* getToken,
|
|
16
|
+
* onNotificationAction,
|
|
17
|
+
* register,
|
|
18
|
+
* requestPermission,
|
|
19
|
+
* } from '@molecule/app-push'
|
|
20
|
+
*
|
|
21
|
+
* async function enablePush(vapidPublicKey: string): Promise<string | null> {
|
|
22
|
+
* const permission = await requestPermission() // from a user gesture
|
|
23
|
+
* if (permission !== 'granted') return null
|
|
24
|
+
* const token = await register({ vapidPublicKey })
|
|
25
|
+
* // POST token.value to your API so api-push-notifications can target it
|
|
26
|
+
* return token.value
|
|
27
|
+
* }
|
|
28
|
+
*
|
|
29
|
+
* function handleTaps(open: (data: unknown) => void): () => void {
|
|
30
|
+
* return onNotificationAction((event) => open(event.notification.data))
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* - **No wiring is needed on web**: the first accessor call silently bonds
|
|
36
|
+
* the built-in Web Push provider. In a native app wire
|
|
37
|
+
* `@molecule/app-push-react-native` (the one prebuilt bond) via
|
|
38
|
+
* `setProvider()` BEFORE any push call — otherwise the web fallback gets
|
|
39
|
+
* bonded and native registration never happens.
|
|
40
|
+
* - **Web `register()` has three hard prerequisites**: (1) HTTPS (secure
|
|
41
|
+
* context), (2) a registered SERVICE WORKER — dev builds typically don't
|
|
42
|
+
* ship one, so register() throws "requires the production app build"
|
|
43
|
+
* (it fails fast instead of hanging), and (3) a VAPID public key
|
|
44
|
+
* (pass via `register({ vapidPublicKey })`) matching your API's
|
|
45
|
+
* `VAPID_PRIVATE_KEY` — without it Chrome may register but delivery
|
|
46
|
+
* fails.
|
|
47
|
+
* - **Request permission from a user gesture at the point of value**, never
|
|
48
|
+
* on load — a denied browser prompt is remembered forever (no re-prompt).
|
|
49
|
+
* - Web `scheduleLocal` at a future time uses an in-page timer: it does NOT
|
|
50
|
+
* survive reload/close. Web `cancelLocal`/`cancelAllLocal` are no-ops,
|
|
51
|
+
* and `getPendingLocal()` is always empty — for reliable scheduled
|
|
52
|
+
* delivery send a real push from the server.
|
|
53
|
+
* - `token.value` on web is a JSON-serialized PushSubscription (not an FCM
|
|
54
|
+
* token) — store it opaquely; platform is in `token.platform`.
|
|
55
|
+
*
|
|
56
|
+
* @module
|
|
57
|
+
*/
|
|
58
|
+
export * from './provider.js';
|
|
59
|
+
export * from './types.js';
|
|
60
|
+
export * from './web-provider.js';
|
|
61
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AAEH,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,mBAAmB,CAAA"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Push provider management for molecule.dev.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { LocalNotificationOptions, NotificationActionListener, NotificationReceivedListener, PermissionStatus, PushProvider, PushRegisterOptions, PushToken } from './types.js';
|
|
7
|
+
/**
|
|
8
|
+
* Sets the push provider implementation.
|
|
9
|
+
* @param provider - The push provider to bond as the active implementation.
|
|
10
|
+
*/
|
|
11
|
+
export declare const setProvider: (provider: PushProvider) => void;
|
|
12
|
+
/**
|
|
13
|
+
* Gets the current push provider, creating a default web provider if none is bonded.
|
|
14
|
+
* @returns The active push provider instance.
|
|
15
|
+
*/
|
|
16
|
+
export declare const getProvider: () => PushProvider;
|
|
17
|
+
/**
|
|
18
|
+
* Checks if a push provider has been bonded.
|
|
19
|
+
* @returns Whether a push provider is currently registered.
|
|
20
|
+
*/
|
|
21
|
+
export declare const hasProvider: () => boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Checks the current notification permission status.
|
|
24
|
+
* @returns The current permission status (granted, denied, default, or prompt).
|
|
25
|
+
*/
|
|
26
|
+
export declare const checkPermission: () => Promise<PermissionStatus>;
|
|
27
|
+
/**
|
|
28
|
+
* Requests notification permission from the user.
|
|
29
|
+
* @returns The resulting permission status after the user responds.
|
|
30
|
+
*/
|
|
31
|
+
export declare const requestPermission: () => Promise<PermissionStatus>;
|
|
32
|
+
/**
|
|
33
|
+
* Registers for push notifications and obtains a device token.
|
|
34
|
+
* @param options - Optional registration options (e.g. a runtime VAPID public key).
|
|
35
|
+
* @returns The push token for this device.
|
|
36
|
+
*/
|
|
37
|
+
export declare const register: (options?: PushRegisterOptions) => Promise<PushToken>;
|
|
38
|
+
/**
|
|
39
|
+
* Gets the current push token.
|
|
40
|
+
* @returns The current token, or `null` if not registered.
|
|
41
|
+
*/
|
|
42
|
+
export declare const getToken: () => Promise<PushToken | null>;
|
|
43
|
+
/**
|
|
44
|
+
* Subscribes to notification received events.
|
|
45
|
+
* @param listener - Callback invoked when a notification is received.
|
|
46
|
+
* @returns An unsubscribe function to remove the listener.
|
|
47
|
+
*/
|
|
48
|
+
export declare const onNotificationReceived: (listener: NotificationReceivedListener) => (() => void);
|
|
49
|
+
/**
|
|
50
|
+
* Subscribes to notification action events (taps, button clicks).
|
|
51
|
+
* @param listener - Callback invoked when a notification action is triggered.
|
|
52
|
+
* @returns An unsubscribe function to remove the listener.
|
|
53
|
+
*/
|
|
54
|
+
export declare const onNotificationAction: (listener: NotificationActionListener) => (() => void);
|
|
55
|
+
/**
|
|
56
|
+
* Schedules a local notification.
|
|
57
|
+
* @param options - Configuration for the notification (title, body, schedule time, etc.).
|
|
58
|
+
* @returns The notification ID that can be used to cancel it later.
|
|
59
|
+
*/
|
|
60
|
+
export declare const scheduleLocal: (options: LocalNotificationOptions) => Promise<string>;
|
|
61
|
+
/**
|
|
62
|
+
* Sets the app icon badge count.
|
|
63
|
+
* @param count - The badge number to display on the app icon.
|
|
64
|
+
* @returns A promise that resolves when the badge count is set.
|
|
65
|
+
*/
|
|
66
|
+
export declare const setBadge: (count: number) => Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* Clears the app icon badge count.
|
|
69
|
+
* @returns A promise that resolves when the badge count is cleared.
|
|
70
|
+
*/
|
|
71
|
+
export declare const clearBadge: () => Promise<void>;
|
|
72
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,4BAA4B,EAC5B,gBAAgB,EAChB,YAAY,EACZ,mBAAmB,EACnB,SAAS,EACV,MAAM,YAAY,CAAA;AAKnB;;;GAGG;AACH,eAAO,MAAM,WAAW,GAAI,UAAU,YAAY,KAAG,IAEpD,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,WAAW,QAAO,YAK9B,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,WAAW,QAAO,OAE9B,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,eAAe,QAAO,OAAO,CAAC,gBAAgB,CAAoC,CAAA;AAE/F;;;GAGG;AACH,eAAO,MAAM,iBAAiB,QAAO,OAAO,CAAC,gBAAgB,CAAsC,CAAA;AAEnG;;;;GAIG;AACH,eAAO,MAAM,QAAQ,GAAI,UAAU,mBAAmB,KAAG,OAAO,CAAC,SAAS,CACzC,CAAA;AAEjC;;;GAGG;AACH,eAAO,MAAM,QAAQ,QAAO,OAAO,CAAC,SAAS,GAAG,IAAI,CAA6B,CAAA;AAEjF;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,GAAI,UAAU,4BAA4B,KAAG,CAAC,MAAM,IAAI,CAC3C,CAAA;AAEhD;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,GAAI,UAAU,0BAA0B,KAAG,CAAC,MAAM,IAAI,CACzC,CAAA;AAE9C;;;;GAIG;AACH,eAAO,MAAM,aAAa,GAAI,SAAS,wBAAwB,KAAG,OAAO,CAAC,MAAM,CAC1C,CAAA;AAEtC;;;;GAIG;AACH,eAAO,MAAM,QAAQ,GAAI,OAAO,MAAM,KAAG,OAAO,CAAC,IAAI,CAAkC,CAAA;AAEvF;;;GAGG;AACH,eAAO,MAAM,UAAU,QAAO,OAAO,CAAC,IAAI,CAA+B,CAAA"}
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Push provider management for molecule.dev.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { bond, get as bondGet, isBonded } from '@molecule/app-bond';
|
|
7
|
+
import { createWebPushProvider } from './web-provider.js';
|
|
8
|
+
const BOND_TYPE = 'push';
|
|
9
|
+
/**
|
|
10
|
+
* Sets the push provider implementation.
|
|
11
|
+
* @param provider - The push provider to bond as the active implementation.
|
|
12
|
+
*/
|
|
13
|
+
export const setProvider = (provider) => {
|
|
14
|
+
bond(BOND_TYPE, provider);
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Gets the current push provider, creating a default web provider if none is bonded.
|
|
18
|
+
* @returns The active push provider instance.
|
|
19
|
+
*/
|
|
20
|
+
export const getProvider = () => {
|
|
21
|
+
if (!isBonded(BOND_TYPE)) {
|
|
22
|
+
bond(BOND_TYPE, createWebPushProvider());
|
|
23
|
+
}
|
|
24
|
+
return bondGet(BOND_TYPE);
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Checks if a push provider has been bonded.
|
|
28
|
+
* @returns Whether a push provider is currently registered.
|
|
29
|
+
*/
|
|
30
|
+
export const hasProvider = () => {
|
|
31
|
+
return isBonded(BOND_TYPE);
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Checks the current notification permission status.
|
|
35
|
+
* @returns The current permission status (granted, denied, default, or prompt).
|
|
36
|
+
*/
|
|
37
|
+
export const checkPermission = () => getProvider().checkPermission();
|
|
38
|
+
/**
|
|
39
|
+
* Requests notification permission from the user.
|
|
40
|
+
* @returns The resulting permission status after the user responds.
|
|
41
|
+
*/
|
|
42
|
+
export const requestPermission = () => getProvider().requestPermission();
|
|
43
|
+
/**
|
|
44
|
+
* Registers for push notifications and obtains a device token.
|
|
45
|
+
* @param options - Optional registration options (e.g. a runtime VAPID public key).
|
|
46
|
+
* @returns The push token for this device.
|
|
47
|
+
*/
|
|
48
|
+
export const register = (options) => getProvider().register(options);
|
|
49
|
+
/**
|
|
50
|
+
* Gets the current push token.
|
|
51
|
+
* @returns The current token, or `null` if not registered.
|
|
52
|
+
*/
|
|
53
|
+
export const getToken = () => getProvider().getToken();
|
|
54
|
+
/**
|
|
55
|
+
* Subscribes to notification received events.
|
|
56
|
+
* @param listener - Callback invoked when a notification is received.
|
|
57
|
+
* @returns An unsubscribe function to remove the listener.
|
|
58
|
+
*/
|
|
59
|
+
export const onNotificationReceived = (listener) => getProvider().onNotificationReceived(listener);
|
|
60
|
+
/**
|
|
61
|
+
* Subscribes to notification action events (taps, button clicks).
|
|
62
|
+
* @param listener - Callback invoked when a notification action is triggered.
|
|
63
|
+
* @returns An unsubscribe function to remove the listener.
|
|
64
|
+
*/
|
|
65
|
+
export const onNotificationAction = (listener) => getProvider().onNotificationAction(listener);
|
|
66
|
+
/**
|
|
67
|
+
* Schedules a local notification.
|
|
68
|
+
* @param options - Configuration for the notification (title, body, schedule time, etc.).
|
|
69
|
+
* @returns The notification ID that can be used to cancel it later.
|
|
70
|
+
*/
|
|
71
|
+
export const scheduleLocal = (options) => getProvider().scheduleLocal(options);
|
|
72
|
+
/**
|
|
73
|
+
* Sets the app icon badge count.
|
|
74
|
+
* @param count - The badge number to display on the app icon.
|
|
75
|
+
* @returns A promise that resolves when the badge count is set.
|
|
76
|
+
*/
|
|
77
|
+
export const setBadge = (count) => getProvider().setBadge(count);
|
|
78
|
+
/**
|
|
79
|
+
* Clears the app icon badge count.
|
|
80
|
+
* @returns A promise that resolves when the badge count is cleared.
|
|
81
|
+
*/
|
|
82
|
+
export const clearBadge = () => getProvider().clearBadge();
|
|
83
|
+
//# sourceMappingURL=provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,IAAI,EAAE,GAAG,IAAI,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAWnE,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAA;AAEzD,MAAM,SAAS,GAAG,MAAM,CAAA;AAExB;;;GAGG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,QAAsB,EAAQ,EAAE;IAC1D,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAA;AAC3B,CAAC,CAAA;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM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CAAC,MAAM,QAAQ,GAAG,CAAC,KAAa,EAAiB,EAAE,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;AAEvF;;;GAGG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,GAAkB,EAAE,CAAC,WAAW,EAAE,CAAC,UAAU,EAAE,CAAA"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Push notification types for molecule.dev.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Push notification permission status.
|
|
8
|
+
*/
|
|
9
|
+
export type PermissionStatus = 'granted' | 'denied' | 'default' | 'prompt';
|
|
10
|
+
/**
|
|
11
|
+
* Push notification payload.
|
|
12
|
+
*/
|
|
13
|
+
export interface PushNotification {
|
|
14
|
+
/**
|
|
15
|
+
* Notification ID.
|
|
16
|
+
*/
|
|
17
|
+
id: string;
|
|
18
|
+
/**
|
|
19
|
+
* Notification title.
|
|
20
|
+
*/
|
|
21
|
+
title: string;
|
|
22
|
+
/**
|
|
23
|
+
* Notification body/message.
|
|
24
|
+
*/
|
|
25
|
+
body?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Notification data payload.
|
|
28
|
+
*/
|
|
29
|
+
data?: Record<string, unknown>;
|
|
30
|
+
/**
|
|
31
|
+
* Badge count.
|
|
32
|
+
*/
|
|
33
|
+
badge?: number;
|
|
34
|
+
/**
|
|
35
|
+
* Sound to play.
|
|
36
|
+
*/
|
|
37
|
+
sound?: string;
|
|
38
|
+
/**
|
|
39
|
+
* Icon URL.
|
|
40
|
+
*/
|
|
41
|
+
icon?: string;
|
|
42
|
+
/**
|
|
43
|
+
* Image URL.
|
|
44
|
+
*/
|
|
45
|
+
image?: string;
|
|
46
|
+
/**
|
|
47
|
+
* Click action/URL.
|
|
48
|
+
*/
|
|
49
|
+
clickAction?: string;
|
|
50
|
+
/**
|
|
51
|
+
* Notification tag (for grouping).
|
|
52
|
+
*/
|
|
53
|
+
tag?: string;
|
|
54
|
+
/**
|
|
55
|
+
* Whether the notification requires interaction.
|
|
56
|
+
*/
|
|
57
|
+
requireInteraction?: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Notification timestamp.
|
|
60
|
+
*/
|
|
61
|
+
timestamp?: number;
|
|
62
|
+
/**
|
|
63
|
+
* Notification actions (buttons).
|
|
64
|
+
*/
|
|
65
|
+
actions?: PushNotificationAction[];
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Notification action button.
|
|
69
|
+
*/
|
|
70
|
+
export interface PushNotificationAction {
|
|
71
|
+
/**
|
|
72
|
+
* Action ID.
|
|
73
|
+
*/
|
|
74
|
+
id: string;
|
|
75
|
+
/**
|
|
76
|
+
* Action title.
|
|
77
|
+
*/
|
|
78
|
+
title: string;
|
|
79
|
+
/**
|
|
80
|
+
* Action icon.
|
|
81
|
+
*/
|
|
82
|
+
icon?: string;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Push token info.
|
|
86
|
+
*/
|
|
87
|
+
export interface PushToken {
|
|
88
|
+
/**
|
|
89
|
+
* Token value.
|
|
90
|
+
*/
|
|
91
|
+
value: string;
|
|
92
|
+
/**
|
|
93
|
+
* Platform the token is for.
|
|
94
|
+
*/
|
|
95
|
+
platform: 'web' | 'ios' | 'android';
|
|
96
|
+
/**
|
|
97
|
+
* When the token was obtained.
|
|
98
|
+
*/
|
|
99
|
+
timestamp: number;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Notification received event.
|
|
103
|
+
*/
|
|
104
|
+
export interface NotificationReceivedEvent {
|
|
105
|
+
/**
|
|
106
|
+
* The notification.
|
|
107
|
+
*/
|
|
108
|
+
notification: PushNotification;
|
|
109
|
+
/**
|
|
110
|
+
* Whether the app was in foreground.
|
|
111
|
+
*/
|
|
112
|
+
foreground: boolean;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Notification action event.
|
|
116
|
+
*/
|
|
117
|
+
export interface NotificationActionEvent {
|
|
118
|
+
/**
|
|
119
|
+
* The notification.
|
|
120
|
+
*/
|
|
121
|
+
notification: PushNotification;
|
|
122
|
+
/**
|
|
123
|
+
* Action that was triggered.
|
|
124
|
+
*/
|
|
125
|
+
actionId?: string;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Listener invoked when a push notification is received.
|
|
129
|
+
* @param event - The received notification and foreground state.
|
|
130
|
+
*/
|
|
131
|
+
export type NotificationReceivedListener = (event: NotificationReceivedEvent) => void;
|
|
132
|
+
/**
|
|
133
|
+
* Notification Action Listener type.
|
|
134
|
+
*/
|
|
135
|
+
export type NotificationActionListener = (event: NotificationActionEvent) => void;
|
|
136
|
+
/**
|
|
137
|
+
* Token Change Listener type.
|
|
138
|
+
*/
|
|
139
|
+
export type TokenChangeListener = (token: PushToken) => void;
|
|
140
|
+
/**
|
|
141
|
+
* Options for scheduling a local notification.
|
|
142
|
+
*/
|
|
143
|
+
export interface LocalNotificationOptions {
|
|
144
|
+
/**
|
|
145
|
+
* Notification ID.
|
|
146
|
+
*/
|
|
147
|
+
id?: string;
|
|
148
|
+
/**
|
|
149
|
+
* Notification title.
|
|
150
|
+
*/
|
|
151
|
+
title: string;
|
|
152
|
+
/**
|
|
153
|
+
* Notification body.
|
|
154
|
+
*/
|
|
155
|
+
body?: string;
|
|
156
|
+
/**
|
|
157
|
+
* Schedule at a specific time.
|
|
158
|
+
*/
|
|
159
|
+
at?: Date;
|
|
160
|
+
/**
|
|
161
|
+
* Repeat interval.
|
|
162
|
+
*/
|
|
163
|
+
repeat?: 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year';
|
|
164
|
+
/**
|
|
165
|
+
* Extra data.
|
|
166
|
+
*/
|
|
167
|
+
extra?: Record<string, unknown>;
|
|
168
|
+
/**
|
|
169
|
+
* Sound to play.
|
|
170
|
+
*/
|
|
171
|
+
sound?: string;
|
|
172
|
+
/**
|
|
173
|
+
* Badge count.
|
|
174
|
+
*/
|
|
175
|
+
badge?: number;
|
|
176
|
+
/**
|
|
177
|
+
* Notification channel (Android).
|
|
178
|
+
*/
|
|
179
|
+
channelId?: string;
|
|
180
|
+
/**
|
|
181
|
+
* Actions/buttons.
|
|
182
|
+
*/
|
|
183
|
+
actions?: PushNotificationAction[];
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Options for {@link PushProvider.register}.
|
|
187
|
+
*/
|
|
188
|
+
export interface PushRegisterOptions {
|
|
189
|
+
/**
|
|
190
|
+
* VAPID public key (URL-safe base64) used as the `applicationServerKey`
|
|
191
|
+
* when subscribing on the web. Chromium rejects keyless subscriptions, so
|
|
192
|
+
* web apps should deliver the server's key at runtime (e.g. from
|
|
193
|
+
* `GET /api/devices/push/public-key`) and pass it here. Takes precedence
|
|
194
|
+
* over any key the provider was constructed with. Ignored by providers
|
|
195
|
+
* whose platform does not use VAPID (e.g. FCM/APNs native providers).
|
|
196
|
+
*/
|
|
197
|
+
vapidPublicKey?: string;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Push notifications provider interface.
|
|
201
|
+
*
|
|
202
|
+
* All push providers must implement this interface.
|
|
203
|
+
*/
|
|
204
|
+
export interface PushProvider {
|
|
205
|
+
/**
|
|
206
|
+
* Checks the current permission status.
|
|
207
|
+
*/
|
|
208
|
+
checkPermission(): Promise<PermissionStatus>;
|
|
209
|
+
/**
|
|
210
|
+
* Requests notification permission.
|
|
211
|
+
*/
|
|
212
|
+
requestPermission(): Promise<PermissionStatus>;
|
|
213
|
+
/**
|
|
214
|
+
* Registers for push notifications and gets a token.
|
|
215
|
+
*
|
|
216
|
+
* @param options - Optional registration options (e.g. a runtime VAPID
|
|
217
|
+
* public key for web subscriptions).
|
|
218
|
+
*/
|
|
219
|
+
register(options?: PushRegisterOptions): Promise<PushToken>;
|
|
220
|
+
/**
|
|
221
|
+
* Unregisters from push notifications.
|
|
222
|
+
*/
|
|
223
|
+
unregister(): Promise<void>;
|
|
224
|
+
/**
|
|
225
|
+
* Gets the current push token.
|
|
226
|
+
* @returns The current token, or `null` if not registered.
|
|
227
|
+
*/
|
|
228
|
+
getToken(): Promise<PushToken | null>;
|
|
229
|
+
/**
|
|
230
|
+
* Subscribes to notification received events.
|
|
231
|
+
*/
|
|
232
|
+
onNotificationReceived(listener: NotificationReceivedListener): () => void;
|
|
233
|
+
/**
|
|
234
|
+
* Subscribes to notification action events (taps, button clicks).
|
|
235
|
+
*/
|
|
236
|
+
onNotificationAction(listener: NotificationActionListener): () => void;
|
|
237
|
+
/**
|
|
238
|
+
* Subscribes to token changes.
|
|
239
|
+
*/
|
|
240
|
+
onTokenChange(listener: TokenChangeListener): () => void;
|
|
241
|
+
/**
|
|
242
|
+
* Schedules a local notification.
|
|
243
|
+
*/
|
|
244
|
+
scheduleLocal(options: LocalNotificationOptions): Promise<string>;
|
|
245
|
+
/**
|
|
246
|
+
* Cancels a local notification.
|
|
247
|
+
*/
|
|
248
|
+
cancelLocal(id: string): Promise<void>;
|
|
249
|
+
/**
|
|
250
|
+
* Cancels all local notifications.
|
|
251
|
+
*/
|
|
252
|
+
cancelAllLocal(): Promise<void>;
|
|
253
|
+
/**
|
|
254
|
+
* Gets pending local notifications.
|
|
255
|
+
*/
|
|
256
|
+
getPendingLocal(): Promise<LocalNotificationOptions[]>;
|
|
257
|
+
/**
|
|
258
|
+
* Gets delivered notifications.
|
|
259
|
+
*/
|
|
260
|
+
getDelivered(): Promise<PushNotification[]>;
|
|
261
|
+
/**
|
|
262
|
+
* Removes delivered notifications.
|
|
263
|
+
*/
|
|
264
|
+
removeDelivered(ids: string[]): Promise<void>;
|
|
265
|
+
/**
|
|
266
|
+
* Removes all delivered notifications.
|
|
267
|
+
*/
|
|
268
|
+
removeAllDelivered(): Promise<void>;
|
|
269
|
+
/**
|
|
270
|
+
* Sets the badge count.
|
|
271
|
+
*/
|
|
272
|
+
setBadge(count: number): Promise<void>;
|
|
273
|
+
/**
|
|
274
|
+
* Gets the badge count.
|
|
275
|
+
*/
|
|
276
|
+
getBadge(): Promise<number>;
|
|
277
|
+
/**
|
|
278
|
+
* Clears the badge.
|
|
279
|
+
*/
|
|
280
|
+
clearBadge(): Promise<void>;
|
|
281
|
+
/**
|
|
282
|
+
* Destroys the provider.
|
|
283
|
+
*/
|
|
284
|
+
destroy(): void;
|
|
285
|
+
}
|
|
286
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,SAAS,GAAG,QAAQ,GAAG,SAAS,GAAG,QAAQ,CAAA;AAE1E;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,EAAE,EAAE,MAAM,CAAA;IAEV;;OAEG;IACH,KAAK,EAAE,MAAM,CAAA;IAEb;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAA;IAEb;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAE9B;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;IAEd;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;IAEd;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAA;IAEb;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;IAEd;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IAEpB;;OAEG;IACH,GAAG,CAAC,EAAE,MAAM,CAAA;IAEZ;;OAEG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAA;IAE5B;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;IAElB;;OAEG;IACH,OAAO,CAAC,EAAE,sBAAsB,EAAE,CAAA;CACnC;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACr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G;IACH,eAAe,IAAI,OAAO,CAAC,wBAAwB,EAAE,CAAC,CAAA;IAEtD;;OAEG;IACH,YAAY,IAAI,OAAO,CAAC,gBAAgB,EAAE,CAAC,CAAA;IAE3C;;OAEG;IACH,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAE7C;;OAEG;IACH,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;IAEnC;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAEtC;;OAEG;IACH,QAAQ,IAAI,OAAO,CAAC,MAAM,CAAC,CAAA;IAE3B;;OAEG;IACH,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;IAE3B;;OAEG;IACH,OAAO,IAAI,IAAI,CAAA;CAChB"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web-based push provider using the Push API.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { PushProvider } from './types.js';
|
|
7
|
+
/**
|
|
8
|
+
* Creates a web push provider using the browser Push API and Notification API.
|
|
9
|
+
* @param vapidPublicKey - Optional VAPID public key for server-authenticated subscriptions.
|
|
10
|
+
* @returns A {@link PushProvider} backed by the Web Push API.
|
|
11
|
+
*/
|
|
12
|
+
export declare const createWebPushProvider: (vapidPublicKey?: string) => PushProvider;
|
|
13
|
+
//# sourceMappingURL=web-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web-provider.d.ts","sourceRoot":"","sources":["../src/web-provider.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAMV,YAAY,EAIb,MAAM,YAAY,CAAA;AAmBnB;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,GAAI,iBAAiB,MAAM,KAAG,YAsS/D,CAAA"}
|