@tencentcloud/web-push 1.0.3 → 1.0.5

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.
Files changed (43) hide show
  1. package/CHANGELOG.md +69 -0
  2. package/README.md +100 -92
  3. package/{dist/index.esm.js → index.esm.js} +1 -1
  4. package/{dist/index.umd.js → index.umd.js} +1 -1
  5. package/package.json +9 -47
  6. package/sw.js +1 -0
  7. package/dist/sw.js +0 -1
  8. package/src/__tests__/index.test.ts +0 -120
  9. package/src/__tests__/integration.test.ts +0 -285
  10. package/src/__tests__/setup.ts +0 -210
  11. package/src/__tests__/types.test.ts +0 -303
  12. package/src/__tests__/web-push-sdk.test.ts +0 -257
  13. package/src/components/message-popup.ts +0 -1007
  14. package/src/core/event-emitter.ts +0 -61
  15. package/src/core/service-worker-manager.ts +0 -614
  16. package/src/core/web-push-sdk.ts +0 -690
  17. package/src/debug/GenerateTestUserSig.js +0 -37
  18. package/src/debug/index.d.ts +0 -6
  19. package/src/debug/index.js +0 -1
  20. package/src/debug/lib-generate-test-usersig-es.min.js +0 -2
  21. package/src/index.ts +0 -9
  22. package/src/service-worker/sw.ts +0 -494
  23. package/src/types/index.ts +0 -2
  24. package/src/types/inner.ts +0 -44
  25. package/src/types/outer.ts +0 -142
  26. package/src/utils/browser-support.ts +0 -412
  27. package/src/utils/logger.ts +0 -66
  28. package/src/utils/storage.ts +0 -51
  29. package/src/utils/validator.ts +0 -267
  30. /package/{dist/index.d.ts → index.d.ts} +0 -0
  31. /package/{dist/src → src}/components/message-popup.d.ts +0 -0
  32. /package/{dist/src → src}/core/event-emitter.d.ts +0 -0
  33. /package/{dist/src → src}/core/service-worker-manager.d.ts +0 -0
  34. /package/{dist/src → src}/core/web-push-sdk.d.ts +0 -0
  35. /package/{dist/src → src}/index.d.ts +0 -0
  36. /package/{dist/src → src}/service-worker/sw.d.ts +0 -0
  37. /package/{dist/src → src}/types/index.d.ts +0 -0
  38. /package/{dist/src → src}/types/inner.d.ts +0 -0
  39. /package/{dist/src → src}/types/outer.d.ts +0 -0
  40. /package/{dist/src → src}/utils/browser-support.d.ts +0 -0
  41. /package/{dist/src → src}/utils/logger.d.ts +0 -0
  42. /package/{dist/src → src}/utils/storage.d.ts +0 -0
  43. /package/{dist/src → src}/utils/validator.d.ts +0 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,69 @@
1
+ # Changelog
2
+
3
+ ## [1.0.5] - 2025-02-02
4
+
5
+ ### Added
6
+
7
+ - Support for webpushInfo ext pass-through fields
8
+
9
+ ## [1.0.4] - 2025-01-27
10
+
11
+ ### Added
12
+
13
+ - **Online Push Popup Feature**: Real-time in-app message popup display to enhance user experience
14
+ - **Multiple Message Type Support**: Standard (standard template), HTML (custom HTML), Custom (fully customizable)
15
+ - **Log Level Control**: Added `logLevel` parameter to registerPush, supporting 5 levels of log control
16
+
17
+ ## [1.0.2] - 2025-01-16
18
+
19
+ - ✨ Added UMD build support for direct `<script>` tag integration
20
+ - 📚 Enhanced UMD usage documentation and examples
21
+
22
+ ## [1.0.1] - 2025-12-19
23
+
24
+ - Optimize subscription logic
25
+ - Optimized state persistence logic, only persisting VAPID public key while resetting temporary states each session
26
+
27
+ ## [1.0.0] - 2025-12-17
28
+
29
+ ### Added
30
+
31
+ - 🎉 Initial release
32
+ - ✨ Service Worker-based push notification support
33
+ - ✨ Complete TypeScript type definitions
34
+ - ✨ Event-driven message handling mechanism
35
+ - ✨ Built-in analytics functionality
36
+ - ✨ Local state persistence
37
+ - ✨ VAPID authentication support
38
+ - ✨ Cross-platform push notification support
39
+
40
+ ### Features
41
+
42
+ - 📱 Support for message received, notification clicked, and message revoked events
43
+ - 🔧 Flexible device identifier management
44
+ - 📊 Automatic message delivery and click rate statistics
45
+ - 🎯 Seamless integration with Chat SDK
46
+ - 💾 Automatic state recovery
47
+ - 🔒 Secure push subscription management
48
+
49
+ ### API Interface
50
+
51
+ - `registerPush()` - Register push service
52
+ - `unRegisterPush()` - Unregister push service
53
+ - `addPushListener()` - Add event listener
54
+ - `removePushListener()` - Remove event listener
55
+
56
+ ### Browser Support
57
+
58
+ - Chrome 50+
59
+ - Firefox 44+
60
+ - Safari 16+
61
+ - Edge 17+
62
+
63
+ ### Development Tools
64
+
65
+ - Vite build tool
66
+ - TypeScript support
67
+ - Jest unit testing
68
+ - ESLint code linting
69
+ - Complete example application
package/README.md CHANGED
@@ -1,50 +1,58 @@
1
- # Web Push SDK
1
+ # Tencent Cloud Web Push SDK
2
2
 
3
- 基于 Web 技术栈的推送服务 SDK,核心使用 Service WorkerPush API Notification API 实现网页推送功能。
3
+ A web-based push notification SDK built on modern Web APIs including Service Worker, Push API, and Notification API.
4
4
 
5
- ## 特性
5
+ ## Features
6
6
 
7
- - 🚀 基于现代 Web 标准 API
8
- - 📱 支持跨平台推送通知
9
- - 💬 **在线推送弹窗**:应用内消息实时弹窗显示
10
- - 🎨 **自定义弹窗样式**:支持位置、样式、行为全方位自定义
11
- - 🔧 TypeScript 支持
12
- - 📊 内置数据统计功能
13
- - 🎯 事件驱动架构
14
- - 💾 本地状态持久化
15
- - 🔒 安全的 VAPID 认证
7
+ - 🚀 Built on modern Web standard APIs
8
+ - 📱 Cross-platform push notification support
9
+ - 💬 **Online Push Popup**: Real-time in-app message popup display
10
+ - 🎨 **Custom Popup Styles**: Full customization support for position, styling, and behavior
11
+ - 🔧 TypeScript support
12
+ - 📊 Built-in analytics functionality
13
+ - 🎯 Event-driven architecture
14
+ - 💾 Local state persistence
15
+ - 🔒 Secure VAPID authentication
16
16
 
17
- ## 浏览器支持
17
+ ## Browser Support
18
18
 
19
19
  - Chrome 50+
20
20
  - Firefox 44+
21
21
  - Safari 16+
22
22
  - Edge 17+
23
23
 
24
- ## 安装
24
+ ## Integration Step
25
25
 
26
- ### 方式一:NPM 安装
26
+ ### Step 1: Install Web Push SDK
27
+
28
+ **Option 1: NPM Installation**
29
+
30
+ ```bash
31
+ npm install @tencentcloud/web-push --save
32
+ ```
33
+
34
+ or
27
35
 
28
36
  ```bash
29
- npm install @tencentcloud/web-push
37
+ yarn add @tencentcloud/web-push
30
38
  ```
31
39
 
32
- ### 方式二:CDN 引入(UMD
40
+ **Option 2: CDN Integration (UMD)**
33
41
 
34
- 通过 `<script>` 标签直接引入使用:
42
+ Include directly via `<script>` tag:
35
43
 
36
44
  ```html
37
- <!-- 引入 TencentCloudChat SDK (专业版) -->
45
+ <!-- Include TencentCloudChat SDK (Professional Edition) -->
38
46
  <script src="https://unpkg.com/@tencentcloud/lite-chat/professional.js"></script>
39
47
 
40
- <!-- 引入 Web Push SDK -->
48
+ <!-- Include Web Push SDK -->
41
49
  <script src="https://unpkg.com/@tencentcloud/web-push/index.umd.js"></script>
42
50
 
43
51
  <script>
44
- // SDK 会自动挂载到全局变量 WebPushSDK
52
+ // SDK will be mounted to global variable WebPushSDK
45
53
  const webPush = WebPushSDK.webPush;
46
54
 
47
- // 使用方式与 ES6 模块相同
55
+ // Usage is the same as ES6 modules
48
56
  webPush.registerPush({
49
57
  SDKAppID: 0,
50
58
  appKey: '',
@@ -53,23 +61,23 @@ npm install @tencentcloud/web-push
53
61
  </script>
54
62
  ```
55
63
 
56
- ### 步骤2:配置 Service Worker 文件
64
+ ### Step 2: Configure the Service Worker File
57
65
 
58
- 集成 `@tencentcloud/web-push` 后,需要将 **Service Workersw.js)**复制到您项目**的根目录**。网站部署后,确保该文件可以通过 `https://your-domain.com/sw.js` 的路径被访问到。否则浏览器将无法注册 **Service Worker**。
66
+ 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**.
59
67
 
60
- > **说明:**
68
+ > **Note:**
61
69
  >
62
- > - sw.js 文件**必须放在网站的根目录**才能正常工作,这是浏览器的安全限制。
63
- > - sw.js 只有在 **HTTPS 连接(或本地开发环境 localhost)**下才能被注册成功。请确保您的生产环境网站支持 **HTTPS**。
70
+ > - The sw.js file **must be placed in the website's root directory** to work properly due to browser security restrictions.
71
+ > - 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**.
64
72
 
65
- **public 目录的作用**:在现代前端项目(如 Vue, React, Next.js 等)中,**public 目录是一个特殊目录,它的内容在构建时不会被编译或改名**。放置在 public 目录的文件会原封不动地被复制到网站的根目录。
73
+ **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.
66
74
 
67
- > **注意:**
75
+ > **Note:**
68
76
  >
69
- > - sw.js 放在 src 或其他目录,它可能会被打包工具(Webpack/Vite)编译、改名(例如变成 sw.123abcde.js),从而无法被正确注册。
70
- > - 如果您的项目**没有 public 目录**(例如老式 HTML 项目),请直接将 **sw.js** 放在 **index.html** 所在的同级目录下,确保构建输出后位于根目录即可。
77
+ > - 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.
78
+ > - 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.
71
79
 
72
- MacOS
80
+ macOS
73
81
 
74
82
  ```bash
75
83
  cp node_modules/@tencentcloud/web-push/dist/sw.js public/sw.js
@@ -81,17 +89,17 @@ cp node_modules/@tencentcloud/web-push/dist/sw.js public/sw.js
81
89
  copy node_modules\@tencentcloud\web-push\dist\sw.js public\sw.js
82
90
  ```
83
91
 
84
- ## 快速开始
92
+ ### Step 3: **Register for Push Services**
85
93
 
86
- 在您的主页面(例如:`index.js`)中,引入 `@tencentcloud/web-push` 并注册。
94
+ In your homepage (for example: `index.js`), add `@tencentcloud/web-push` and register.
87
95
 
88
96
  <table>
89
97
  <tr>
90
- <td rowspan="1" colSpan="1" >参数</td>
98
+ <td rowspan="1" colSpan="1" >Parameter</td>
91
99
 
92
- <td rowspan="1" colSpan="1" >类型</td>
100
+ <td rowspan="1" colSpan="1" >Type</td>
93
101
 
94
- <td rowspan="1" colSpan="1" >说明</td>
102
+ <td rowspan="1" colSpan="1" >Description</td>
95
103
  </tr>
96
104
 
97
105
  <tr>
@@ -99,7 +107,7 @@ copy node_modules\@tencentcloud\web-push\dist\sw.js public\sw.js
99
107
 
100
108
  <td rowspan="1" colSpan="1" >Number</td>
101
109
 
102
- <td rowspan="1" colSpan="1" >推送服务 Push 的 SDKAppID。</td>
110
+ <td rowspan="1" colSpan="1" >The SDKAppID for the push service Push. </td>
103
111
  </tr>
104
112
 
105
113
  <tr>
@@ -107,7 +115,7 @@ copy node_modules\@tencentcloud\web-push\dist\sw.js public\sw.js
107
115
 
108
116
  <td rowspan="1" colSpan="1" >String</td>
109
117
 
110
- <td rowspan="1" colSpan="1" >推送服务 Push 的客户端密钥。</td>
118
+ <td rowspan="1" colSpan="1" >The client key for the push service Push.</td>
111
119
  </tr>
112
120
 
113
121
  <tr>
@@ -115,36 +123,36 @@ copy node_modules\@tencentcloud\web-push\dist\sw.js public\sw.js
115
123
 
116
124
  <td rowspan="1" colSpan="1" >String</td>
117
125
 
118
- <td rowspan="1" colSpan="1" >注册推送用户的 userID。用户的唯一标识符,由您定义,只能包含大小写字母(a-zA-Z)、数字(0-9)、下划线和连字符。</td>
126
+ <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>
119
127
  </tr>
120
128
  </table>
121
129
 
122
130
  ```javascript
123
131
  import webPush from '@tencentcloud/web-push';
124
132
 
125
- const SDKAppID = 0; // 您的 SDKAppID
126
- const appKey = ''; // 客户端密钥
127
- const userID = ''; // 用户 ID
133
+ const SDKAppID = 0; // Your SDKAppID
134
+ const appKey = ''; // client key
135
+ const userID = ''; // user ID
128
136
 
129
- // 注册推送服务
137
+ // Register push service
130
138
  await webPush.registerPush({ SDKAppID, appKey, userID });
131
139
 
132
- // 监听推送消息
140
+ // Listen to push messages
133
141
  webPush.addPushListener(webPush.EVENT.MESSAGE_RECEIVED, (message) => {
134
- console.log('收到推送消息:', message);
142
+ console.log('received push message:', message);
135
143
  });
136
144
 
137
- // 监听通知点击
145
+ // Listen to notification click
138
146
  webPush.addPushListener(webPush.EVENT.NOTIFICATION_CLICKED, (data) => {
139
- console.log('通知被点击:', data);
147
+ console.log('notification clicked:', data);
140
148
  });
141
149
  ```
142
150
 
143
- ### UMD 方式(Script 标签)
151
+ ### UMD Usage (Script Tag)
144
152
 
145
- > **重要提示**: 使用 UMD 方式时,必须先引入 TencentCloudChat SDK (专业版),否则会出现 "Cannot read properties of undefined (reading 'create')" 错误。
153
+ > **Important**: When using UMD integration, you must include the TencentCloudChat SDK (Professional Edition) first, otherwise you'll encounter "Cannot read properties of undefined (reading 'create')" error.
146
154
 
147
- 如果您使用 UMD 方式引入,可以参考以下示例:
155
+ If you're using UMD integration, here's an example:
148
156
 
149
157
  ```html
150
158
  <!DOCTYPE html>
@@ -153,57 +161,57 @@ webPush.addPushListener(webPush.EVENT.NOTIFICATION_CLICKED, (data) => {
153
161
  <title>Web Push SDK UMD Example</title>
154
162
  </head>
155
163
  <body>
156
- <!-- 引入 TencentCloudChat SDK (专业版) -->
164
+ <!-- Include TencentCloudChat SDK (Professional Edition) -->
157
165
  <script src="https://unpkg.com/@tencentcloud/lite-chat/professional.js"></script>
158
166
 
159
- <!-- 引入 Web Push SDK -->
167
+ <!-- Include Web Push SDK -->
160
168
  <script src="https://unpkg.com/@tencentcloud/web-push/index.umd.js"></script>
161
169
 
162
170
  <script>
163
- // 获取 SDK 实例
171
+ // Get SDK instance
164
172
  const webPush = WebPushSDK.webPush;
165
173
 
166
- // 配置参数
167
- const SDKAppID = 0; // 您的 SDKAppID
168
- const appKey = 'xxx'; // 客户端密钥
169
- const userID = 'xxx'; // 用户 ID
174
+ // Configuration parameters
175
+ const SDKAppID = 0; // Your SDKAppID
176
+ const appKey = ''; // Client key
177
+ const userID = ''; // User ID
170
178
 
171
- // 注册推送服务
179
+ // Register push service
172
180
  webPush
173
181
  .registerPush({ SDKAppID, appKey, userID })
174
182
  .then((registrationID) => {
175
- console.log('注册成功:', registrationID);
183
+ console.log('Registration successful:', registrationID);
176
184
  })
177
185
  .catch((error) => {
178
- console.error('注册失败:', error);
186
+ console.error('Registration failed:', error);
179
187
  });
180
188
 
181
- // 监听推送消息
189
+ // Listen to push messages
182
190
  webPush.addPushListener(webPush.EVENT.MESSAGE_RECEIVED, (message) => {
183
- console.log('收到推送消息:', message);
191
+ console.log('Received push message:', message);
184
192
  });
185
193
 
186
- // 监听通知点击
194
+ // Listen to notification click
187
195
  webPush.addPushListener(webPush.EVENT.NOTIFICATION_CLICKED, (data) => {
188
- console.log('通知被点击:', data);
196
+ console.log('Notification clicked:', data);
189
197
  });
190
198
  </script>
191
199
  </body>
192
200
  </html>
193
201
  ```
194
202
 
195
- ## API 文档
203
+ ## API Reference
196
204
 
197
- ### 接口说明
205
+ ### Methods
198
206
 
199
- | API | 参数 | 描述 |
200
- | -------------------- | --------------------------------------------------- | ------------------ |
201
- | `registerPush` | `options: RegisterPushOptions` | 注册推送服务 |
202
- | `unRegisterPush` | - | 反注册关闭推送服务 |
203
- | `addPushListener` | `eventName: EVENT, listener: Function<EventResult>` | 添加推送监听器 |
204
- | `removePushListener` | `eventName: EVENT, listener: Function<EventResult>` | 移除推送监听器 |
207
+ | API | Parameters | Description |
208
+ | -------------------- | --------------------------------------------------- | -------------------------- |
209
+ | `registerPush` | `options: RegisterPushOptions` | Register push service |
210
+ | `unRegisterPush` | - | Unregister push service |
211
+ | `addPushListener` | `eventName: EVENT, listener: Function<EventResult>` | Add push event listener |
212
+ | `removePushListener` | `eventName: EVENT, listener: Function<EventResult>` | Remove push event listener |
205
213
 
206
- ### 类型定义
214
+ ### Type Definitions
207
215
 
208
216
  ```typescript
209
217
  interface RegisterPushOptions {
@@ -212,12 +220,12 @@ interface RegisterPushOptions {
212
220
  userID: string;
213
221
  chat?: any;
214
222
  /**
215
- * 日志级别:
216
- * 0 - 普通级别,日志量较多,接入时建议使用
217
- * 1 - release级别,SDK 输出关键信息,生产环境时建议使用(默认)
218
- * 2 - 告警级别,SDK 只输出告警和错误级别的日志
219
- * 3 - 错误级别,SDK 只输出错误级别的日志
220
- * 4 - 无日志级别,SDK 将不打印任何日志
223
+ * Log level:
224
+ * 0 - Normal level, more logs, recommended for integration
225
+ * 1 - Release level, SDK outputs key information, recommended for production (default)
226
+ * 2 - Warning level, SDK only outputs warning and error level logs
227
+ * 3 - Error level, SDK only outputs error level logs
228
+ * 4 - No log level, SDK will not print any logs
221
229
  */
222
230
  logLevel?: LogLevel;
223
231
  }
@@ -229,26 +237,26 @@ enum EVENT {
229
237
  }
230
238
  ```
231
239
 
232
- ### 事件类型
240
+ ### Event Types
233
241
 
234
- - `MESSAGE_RECEIVED`: 接收到推送消息(包括所有类型:standardhtmlcustom
235
- - `MESSAGE_REVOKED`: 消息被撤回
236
- - `NOTIFICATION_CLICKED`: 通知被点击
242
+ - `MESSAGE_RECEIVED`: Received a push message (including all types: standard, html, custom)
243
+ - `MESSAGE_REVOKED`: Message was revoked
244
+ - `NOTIFICATION_CLICKED`: Notification was clicked
237
245
 
238
- ## 注意事项
246
+ ## Important Notes
239
247
 
240
- ### Service Worker 作用域
248
+ ### Service Worker Scope
241
249
 
242
- Service Worker 必须放在网站根目录下(如 `/sw.js`),这样才能控制整个网站的推送消息。如果放在子目录下,将无法正常工作。
250
+ 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.
243
251
 
244
- ### HTTPS 要求
252
+ ### HTTPS Requirement
245
253
 
246
- Web Push API 要求网站必须使用 HTTPS 协议(本地开发环境的 localhost 除外)。
254
+ The Web Push API requires websites to use the HTTPS protocol (except for local development environments using localhost).
247
255
 
248
- ### 浏览器权限
256
+ ### Browser Permissions
249
257
 
250
- 用户首次使用时需要授权通知权限,请在合适的时机引导用户授权。
258
+ Users need to authorize notification permissions upon first use. Please guide users to authorize permissions at appropriate times.
251
259
 
252
- ## 许可证
260
+ ## License
253
261
 
254
262
  MIT License
@@ -1,5 +1,5 @@
1
1
  import ChatSDK from "@tencentcloud/lite-chat/professional";
2
- const version = "1.0.3";
2
+ const version = "1.0.5";
3
3
  var Placement = /* @__PURE__ */ ((Placement2) => {
4
4
  Placement2[Placement2["CENTER"] = 0] = "CENTER";
5
5
  Placement2[Placement2["TOP_LEFT"] = 1] = "TOP_LEFT";