@tencentcloud/web-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/CHANGELOG.md ADDED
@@ -0,0 +1,45 @@
1
+ # Changelog
2
+
3
+ ## [1.0.0] - 2025-12-17
4
+
5
+ ### Added
6
+
7
+ - πŸŽ‰ Initial release
8
+ - ✨ Service Worker-based push notification support
9
+ - ✨ Complete TypeScript type definitions
10
+ - ✨ Event-driven message handling mechanism
11
+ - ✨ Built-in analytics functionality
12
+ - ✨ Local state persistence
13
+ - ✨ VAPID authentication support
14
+ - ✨ Cross-platform push notification support
15
+
16
+ ### Features
17
+
18
+ - πŸ“± Support for message received, notification clicked, and message revoked events
19
+ - πŸ”§ Flexible device identifier management
20
+ - πŸ“Š Automatic message delivery and click rate statistics
21
+ - 🎯 Seamless integration with Chat SDK
22
+ - πŸ’Ύ Automatic state recovery
23
+ - πŸ”’ Secure push subscription management
24
+
25
+ ### API Interface
26
+
27
+ - `registerPush()` - Register push service
28
+ - `unRegisterPush()` - Unregister push service
29
+ - `addPushListener()` - Add event listener
30
+ - `removePushListener()` - Remove event listener
31
+
32
+ ### Browser Support
33
+
34
+ - Chrome 50+
35
+ - Firefox 44+
36
+ - Safari 16+
37
+ - Edge 17+
38
+
39
+ ### Development Tools
40
+
41
+ - Vite build tool
42
+ - TypeScript support
43
+ - Jest unit testing
44
+ - ESLint code linting
45
+ - Complete example application
package/README.md ADDED
@@ -0,0 +1,241 @@
1
+ # Tencent Cloud Web Push SDK
2
+
3
+ A web-based push notification SDK built on modern Web APIs including Service Worker, Push API, and Notification API.
4
+
5
+ ## Features
6
+
7
+ - πŸš€ Built on modern Web standard APIs
8
+ - πŸ“± Cross-platform push notification support
9
+ - πŸ”§ TypeScript support
10
+ - πŸ“Š Built-in analytics functionality
11
+ - 🎯 Event-driven architecture
12
+ - πŸ’Ύ Local state persistence
13
+ - πŸ”’ Secure VAPID authentication
14
+
15
+ ## Browser Support
16
+
17
+ - Chrome 50+
18
+ - Firefox 44+
19
+ - Safari 16+
20
+ - Edge 17+
21
+
22
+ ## Integration Step
23
+
24
+ ### Step 1: Integrating @Tencentcloud/Web-Push
25
+
26
+ 【npm】
27
+
28
+ ```bash
29
+ npm install @tencentcloud/web-push --save
30
+ ```
31
+
32
+ 【yarn】
33
+
34
+ ```bash
35
+ yarn add @tencentcloud/web-push
36
+ ```
37
+
38
+ ### Step 2: Configure the Service Worker File
39
+
40
+ After integrating `@tencentcloud/web-push`, copy the **Service Worker (sw.js)** to your project's **root directory**. After website deployment, ensure this file can be accessed through `https://your-domain.com/sw.js`. Otherwise, the browser will be unable to register the **Service Worker**.
41
+
42
+ > **Note:**
43
+ >
44
+ > - The sw.js file **must be placed in the website's root directory** to work properly due to browser security restrictions.
45
+ > - The sw.js file can only be registered successfully under **HTTPS connection (or local development environment localhost)**. Ensure your live production environment website supports **HTTPS**.
46
+
47
+ **Role of the public directory**: In modern front-end projects (such as Vue, React, Next.js, etc.), **the public directory is a unique directory whose content will not be compiled or renamed during build**. Files placed in the public directory will be copied to the website's root directory as-is.
48
+
49
+ > **Note:**
50
+ >
51
+ > - Place sw.js in the src or other directories, and it may be compiled or renamed (such as changed into sw.123abcde.js) by packaging tools (Webpack/Vite), thereby failing to register correctly.
52
+ > - If your project **does not have a public directory** (such as old-style HTML projects), place **sw.js** directly in the same directory as **index.html** to ensure it is located in the root directory after build output.
53
+
54
+ 【macOS】
55
+
56
+ ```bash
57
+ cp node_modules/@tencentcloud/web-push/dist/sw.js public/sw.js
58
+ ```
59
+
60
+ 【Windows】
61
+
62
+ ```bash
63
+ copy node_modules\@tencentcloud\web-push\dist\sw.js public\sw.js
64
+ ```
65
+
66
+ ### Step 3: **Register for Push Services**
67
+
68
+ In your homepage (for example: `index.js`), add `@tencentcloud/web-push` and register.
69
+
70
+ <table>
71
+ <tr>
72
+ <td rowspan="1" colSpan="1" >Parameter</td>
73
+
74
+ <td rowspan="1" colSpan="1" >Type</td>
75
+
76
+ <td rowspan="1" colSpan="1" >Description</td>
77
+ </tr>
78
+
79
+ <tr>
80
+ <td rowspan="1" colSpan="1" >SDKAppID</td>
81
+
82
+ <td rowspan="1" colSpan="1" >Number</td>
83
+
84
+ <td rowspan="1" colSpan="1" >The SDKAppID for the push service Push. For reference: [Prerequisites > Enabling a Service](https://write.woa.com/#ef1e073e-23da-422d-b06b-f13fcda46734) to obtain the SDKAppID.</td>
85
+ </tr>
86
+
87
+ <tr>
88
+ <td rowspan="1" colSpan="1" >appKey</td>
89
+
90
+ <td rowspan="1" colSpan="1" >String</td>
91
+
92
+ <td rowspan="1" colSpan="1" >The client key for the push service Push. For reference: [Prerequisites > Enabling a Service](https://write.woa.com/#ef1e073e-23da-422d-b06b-f13fcda46734) to obtain the AppKey.</td>
93
+ </tr>
94
+
95
+ <tr>
96
+ <td rowspan="1" colSpan="1" >userID</td>
97
+
98
+ <td rowspan="1" colSpan="1" >String</td>
99
+
100
+ <td rowspan="1" colSpan="1" >Register the userID for push services. User's Unique Identifier, defined by you, can only include uppercase and lowercase letters (a-z, A-Z), numbers (0-9), underscores, and hyphens.</td>
101
+ </tr>
102
+ </table>
103
+
104
+ ```javascript
105
+ import WebPush from '@tencentcloud/web-push';
106
+
107
+ const SDKAppID = 0; // Your SDKAppID
108
+ const appKey = ''; // client key
109
+ const userID = ''; // user ID
110
+
111
+ // Register for push service
112
+ await WebPush.registerPush({ SDKAppID, appKey, userID });
113
+
114
+ // Listen to push messages
115
+ WebPush.addPushListener(WebPush.EVENT.MESSAGE_RECEIVED, (message) => {
116
+ console.log('received push message:', message);
117
+ });
118
+
119
+ // Listen to notification click
120
+ WebPush.addPushListener(WebPush.EVENT.NOTIFICATION_CLICKED, (data) => {
121
+ console.log('notification clicked:', data);
122
+ });
123
+ ```
124
+
125
+ ## API Reference
126
+
127
+ ### Methods
128
+
129
+ | API | Parameters | Description |
130
+ | -------------------- | --------------------------------------------------- | -------------------------- |
131
+ | `registerPush` | `options: RegisterPushOptions` | Register push service |
132
+ | `unRegisterPush` | - | Unregister push service |
133
+ | `addPushListener` | `eventName: EVENT, listener: Function<EventResult>` | Add push event listener |
134
+ | `removePushListener` | `eventName: EVENT, listener: Function<EventResult>` | Remove push event listener |
135
+
136
+ ### Type Definitions
137
+
138
+ ```typescript
139
+ interface RegisterPushOptions {
140
+ SDKAppID: number;
141
+ appKey: string;
142
+ userID: string;
143
+ serviceWorkerPath?: string;
144
+ chat?: any;
145
+ }
146
+
147
+ enum EVENT {
148
+ MESSAGE_RECEIVED = 'message_received',
149
+ MESSAGE_REVOKED = 'message_revoked',
150
+ NOTIFICATION_CLICKED = 'notification_clicked',
151
+ }
152
+
153
+ interface Message {
154
+ ID: string;
155
+ type:
156
+ | 'TIMTextElem'
157
+ | 'TIMImageElem'
158
+ | 'TIMSoundElem'
159
+ | 'TIMVideoFileElem'
160
+ | 'TIMFileElem'
161
+ | 'TIMCustomElem'
162
+ | 'TIMRelayElem'
163
+ | 'TIMLocationElem'
164
+ | 'TIMFaceElem';
165
+ payload: any;
166
+ conversationID: string;
167
+ conversationType: 'C2C' | 'GROUP';
168
+ to: string;
169
+ from: string;
170
+ flow: string;
171
+ time: number;
172
+ status: string;
173
+ isRevoked: boolean;
174
+ nick: string;
175
+ avatar: string;
176
+ isPeerRead: boolean;
177
+ nameCard: string;
178
+ atUserList: string[];
179
+ cloudCustomData: string;
180
+ isDeleted: boolean;
181
+ isModified: boolean;
182
+ needReadReceipt: boolean;
183
+ readReceiptInfo: any;
184
+ isBroadcastMessage: boolean;
185
+ isSupportExtension: boolean;
186
+ receiverList?: string[];
187
+ revoker: string;
188
+ sequence: number;
189
+ progress: number;
190
+ revokerInfo: {
191
+ userID: string;
192
+ nick: string;
193
+ avatar: string;
194
+ };
195
+ revokeReason: string;
196
+ hasRiskContent: boolean;
197
+ }
198
+
199
+ interface MessageReceivedResult {
200
+ message: Message;
201
+ }
202
+
203
+ interface MessageRevokedResult {
204
+ messageID: string;
205
+ }
206
+
207
+ interface MessageNotificationClickedResult {
208
+ ext: string;
209
+ }
210
+
211
+ interface EventResult {
212
+ data:
213
+ | MessageReceivedResult
214
+ | MessageRevokedResult
215
+ | MessageNotificationClickedResult;
216
+ }
217
+ ```
218
+
219
+ ### Event Types
220
+
221
+ - `MESSAGE_RECEIVED`: Received a push message
222
+ - `MESSAGE_REVOKED`: Message was revoked
223
+ - `NOTIFICATION_CLICKED`: Notification was clicked
224
+
225
+ ## Important Notes
226
+
227
+ ### Service Worker Scope
228
+
229
+ Service Workers must be placed in the website's root directory (e.g., `/sw.js`) to control push messages across the entire website. Placing them in subdirectories will not function correctly.
230
+
231
+ ### HTTPS Requirement
232
+
233
+ The Web Push API requires websites to use the HTTPS protocol (except for local development environments using localhost).
234
+
235
+ ### Browser Permissions
236
+
237
+ Users need to authorize notification permissions upon first use. Please guide users to authorize permissions at appropriate times.
238
+
239
+ ## License
240
+
241
+ MIT License
package/index.d.ts ADDED
@@ -0,0 +1,112 @@
1
+ export declare enum EVENT {
2
+ MESSAGE_RECEIVED = "message_received",
3
+ MESSAGE_REVOKED = "message_revoked",
4
+ NOTIFICATION_CLICKED = "notification_clicked"
5
+ }
6
+
7
+ export declare interface EventResult {
8
+ data: MessageReceivedResult | MessageRevokedResult | MessageNotificationClickedResult;
9
+ }
10
+
11
+ export declare interface Message {
12
+ ID: String;
13
+ type: 'TIMTextElem' | 'TIMImageElem' | 'TIMSoundElem' | 'TIMVideoFileElem' | 'TIMFileElem' | 'TIMCustomElem' | 'TIMRelayElem' | 'TIMLocationElem' | 'TIMFaceElem';
14
+ payload: any;
15
+ conversationID: String;
16
+ conversationType: 'C2C' | 'GROUP';
17
+ to: String;
18
+ from: String;
19
+ flow: String;
20
+ time: Number;
21
+ status: String;
22
+ isRevoked: Boolean;
23
+ nick: String;
24
+ avatar: String;
25
+ isPeerRead: Boolean;
26
+ nameCard: String;
27
+ atUserList: String[];
28
+ cloudCustomData: String;
29
+ isDeleted: Boolean;
30
+ isModified: Boolean;
31
+ needReadReceipt: Boolean;
32
+ readReceiptInfo: any;
33
+ isBroadcastMessage: Boolean;
34
+ isSupportExtension: Boolean;
35
+ receiverList?: String[];
36
+ revoker: String;
37
+ sequence: Number;
38
+ progress: Number;
39
+ revokerInfo: {
40
+ userID: String;
41
+ nick: String;
42
+ avatar: String;
43
+ };
44
+ revokeReason: String;
45
+ hasRiskContent: Boolean;
46
+ }
47
+
48
+ export declare interface MessageNotificationClickedResult {
49
+ ext: String;
50
+ }
51
+
52
+ export declare interface MessageReceivedResult {
53
+ message: Message;
54
+ }
55
+
56
+ export declare interface MessageRevokedResult {
57
+ messageID: String;
58
+ }
59
+
60
+ export declare interface RegisterPushOptions {
61
+ SDKAppID: number;
62
+ appKey: string;
63
+ userID: string;
64
+ serviceWorkerPath?: string;
65
+ chat?: any;
66
+ }
67
+
68
+ declare const webPush: WebPushSDK_2;
69
+ export default webPush;
70
+ export { webPush }
71
+
72
+ export declare interface WebPushSDK {
73
+ registerPush(options?: RegisterPushOptions): Promise<any>;
74
+ unRegisterPush(): Promise<any>;
75
+ addPushListener(eventName: EVENT, listener: (data: EventResult) => void): any;
76
+ removePushListener(eventName: EVENT, listener: (data: EventResult) => void): any;
77
+ }
78
+
79
+ declare class WebPushSDK_2 implements WebPushSDK {
80
+ static instance: WebPushSDK_2;
81
+ private eventEmitter;
82
+ private serviceWorkerManager;
83
+ private isRegistered;
84
+ private registrationID;
85
+ private chat;
86
+ private SDKAppID;
87
+ private appKey;
88
+ private vapidPublicKey;
89
+ EVENT: typeof EVENT;
90
+ VERSION: string;
91
+ constructor();
92
+ static getInstance(): WebPushSDK_2;
93
+ registerPush(options?: RegisterPushOptions): Promise<string>;
94
+ unRegisterPush(): Promise<boolean>;
95
+ addPushListener(eventName: EVENT, listener: Function): string;
96
+ removePushListener(eventName: EVENT, listener: Function): boolean;
97
+ private checkBrowserSupport;
98
+ private requestNotificationPermission;
99
+ private chatLogin;
100
+ private getSystemPermissionMessage;
101
+ private showSystemPermissionAlert;
102
+ private getBrowserInstType;
103
+ private setToken;
104
+ private initializeBrowserCompatibility;
105
+ private setupInternalListeners;
106
+ private pushStatistics;
107
+ private saveState;
108
+ private restoreState;
109
+ private clearState;
110
+ }
111
+
112
+ export { }