@tencentcloud/web-push 1.0.3 → 1.0.4
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 +63 -0
- package/README.md +100 -92
- package/{dist/index.esm.js → index.esm.js} +1 -1
- package/{dist/index.umd.js → index.umd.js} +1 -1
- package/package.json +9 -47
- package/src/__tests__/index.test.ts +0 -120
- package/src/__tests__/integration.test.ts +0 -285
- package/src/__tests__/setup.ts +0 -210
- package/src/__tests__/types.test.ts +0 -303
- package/src/__tests__/web-push-sdk.test.ts +0 -257
- package/src/components/message-popup.ts +0 -1007
- package/src/core/event-emitter.ts +0 -61
- package/src/core/service-worker-manager.ts +0 -614
- package/src/core/web-push-sdk.ts +0 -690
- package/src/debug/GenerateTestUserSig.js +0 -37
- package/src/debug/index.d.ts +0 -6
- package/src/debug/index.js +0 -1
- package/src/debug/lib-generate-test-usersig-es.min.js +0 -2
- package/src/index.ts +0 -9
- package/src/service-worker/sw.ts +0 -494
- package/src/types/index.ts +0 -2
- package/src/types/inner.ts +0 -44
- package/src/types/outer.ts +0 -142
- package/src/utils/browser-support.ts +0 -412
- package/src/utils/logger.ts +0 -66
- package/src/utils/storage.ts +0 -51
- package/src/utils/validator.ts +0 -267
- /package/{dist/index.d.ts → index.d.ts} +0 -0
- /package/{dist/src → src}/components/message-popup.d.ts +0 -0
- /package/{dist/src → src}/core/event-emitter.d.ts +0 -0
- /package/{dist/src → src}/core/service-worker-manager.d.ts +0 -0
- /package/{dist/src → src}/core/web-push-sdk.d.ts +0 -0
- /package/{dist/src → src}/index.d.ts +0 -0
- /package/{dist/src → src}/service-worker/sw.d.ts +0 -0
- /package/{dist/src → src}/types/index.d.ts +0 -0
- /package/{dist/src → src}/types/inner.d.ts +0 -0
- /package/{dist/src → src}/types/outer.d.ts +0 -0
- /package/{dist/src → src}/utils/browser-support.d.ts +0 -0
- /package/{dist/src → src}/utils/logger.d.ts +0 -0
- /package/{dist/src → src}/utils/storage.d.ts +0 -0
- /package/{dist/src → src}/utils/validator.d.ts +0 -0
- /package/{dist/sw.js → sw.js} +0 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [1.0.4] - 2025-01-27
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- **Online Push Popup Feature**: Real-time in-app message popup display to enhance user experience
|
|
8
|
+
- **Multiple Message Type Support**: Standard (standard template), HTML (custom HTML), Custom (fully customizable)
|
|
9
|
+
- **Log Level Control**: Added `logLevel` parameter to registerPush, supporting 5 levels of log control
|
|
10
|
+
|
|
11
|
+
## [1.0.2] - 2025-01-16
|
|
12
|
+
|
|
13
|
+
- ✨ Added UMD build support for direct `<script>` tag integration
|
|
14
|
+
- 📚 Enhanced UMD usage documentation and examples
|
|
15
|
+
|
|
16
|
+
## [1.0.1] - 2025-12-19
|
|
17
|
+
|
|
18
|
+
- Optimize subscription logic
|
|
19
|
+
- Optimized state persistence logic, only persisting VAPID public key while resetting temporary states each session
|
|
20
|
+
|
|
21
|
+
## [1.0.0] - 2025-12-17
|
|
22
|
+
|
|
23
|
+
### Added
|
|
24
|
+
|
|
25
|
+
- 🎉 Initial release
|
|
26
|
+
- ✨ Service Worker-based push notification support
|
|
27
|
+
- ✨ Complete TypeScript type definitions
|
|
28
|
+
- ✨ Event-driven message handling mechanism
|
|
29
|
+
- ✨ Built-in analytics functionality
|
|
30
|
+
- ✨ Local state persistence
|
|
31
|
+
- ✨ VAPID authentication support
|
|
32
|
+
- ✨ Cross-platform push notification support
|
|
33
|
+
|
|
34
|
+
### Features
|
|
35
|
+
|
|
36
|
+
- 📱 Support for message received, notification clicked, and message revoked events
|
|
37
|
+
- 🔧 Flexible device identifier management
|
|
38
|
+
- 📊 Automatic message delivery and click rate statistics
|
|
39
|
+
- 🎯 Seamless integration with Chat SDK
|
|
40
|
+
- 💾 Automatic state recovery
|
|
41
|
+
- 🔒 Secure push subscription management
|
|
42
|
+
|
|
43
|
+
### API Interface
|
|
44
|
+
|
|
45
|
+
- `registerPush()` - Register push service
|
|
46
|
+
- `unRegisterPush()` - Unregister push service
|
|
47
|
+
- `addPushListener()` - Add event listener
|
|
48
|
+
- `removePushListener()` - Remove event listener
|
|
49
|
+
|
|
50
|
+
### Browser Support
|
|
51
|
+
|
|
52
|
+
- Chrome 50+
|
|
53
|
+
- Firefox 44+
|
|
54
|
+
- Safari 16+
|
|
55
|
+
- Edge 17+
|
|
56
|
+
|
|
57
|
+
### Development Tools
|
|
58
|
+
|
|
59
|
+
- Vite build tool
|
|
60
|
+
- TypeScript support
|
|
61
|
+
- Jest unit testing
|
|
62
|
+
- ESLint code linting
|
|
63
|
+
- Complete example application
|
package/README.md
CHANGED
|
@@ -1,50 +1,58 @@
|
|
|
1
|
-
# Web Push SDK
|
|
1
|
+
# Tencent Cloud Web Push SDK
|
|
2
2
|
|
|
3
|
-
|
|
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
|
-
- 🚀
|
|
8
|
-
- 📱
|
|
9
|
-
- 💬
|
|
10
|
-
- 🎨
|
|
11
|
-
- 🔧 TypeScript
|
|
12
|
-
- 📊
|
|
13
|
-
- 🎯
|
|
14
|
-
- 💾
|
|
15
|
-
- 🔒
|
|
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
|
-
###
|
|
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
|
-
|
|
37
|
+
yarn add @tencentcloud/web-push
|
|
30
38
|
```
|
|
31
39
|
|
|
32
|
-
|
|
40
|
+
**Option 2: CDN Integration (UMD)**
|
|
33
41
|
|
|
34
|
-
|
|
42
|
+
Include directly via `<script>` tag:
|
|
35
43
|
|
|
36
44
|
```html
|
|
37
|
-
<!--
|
|
45
|
+
<!-- Include TencentCloudChat SDK (Professional Edition) -->
|
|
38
46
|
<script src="https://unpkg.com/@tencentcloud/lite-chat/professional.js"></script>
|
|
39
47
|
|
|
40
|
-
<!--
|
|
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
|
|
52
|
+
// SDK will be mounted to global variable WebPushSDK
|
|
45
53
|
const webPush = WebPushSDK.webPush;
|
|
46
54
|
|
|
47
|
-
//
|
|
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
|
-
###
|
|
64
|
+
### Step 2: Configure the Service Worker File
|
|
57
65
|
|
|
58
|
-
|
|
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
|
|
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
|
|
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
|
-
> -
|
|
70
|
-
> -
|
|
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
|
-
【
|
|
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
|
-
|
|
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"
|
|
98
|
+
<td rowspan="1" colSpan="1" >Parameter</td>
|
|
91
99
|
|
|
92
|
-
<td rowspan="1" colSpan="1"
|
|
100
|
+
<td rowspan="1" colSpan="1" >Type</td>
|
|
93
101
|
|
|
94
|
-
<td rowspan="1" colSpan="1"
|
|
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"
|
|
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"
|
|
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"
|
|
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; //
|
|
126
|
-
const appKey = ''; //
|
|
127
|
-
const userID = ''; //
|
|
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('
|
|
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('
|
|
147
|
+
console.log('notification clicked:', data);
|
|
140
148
|
});
|
|
141
149
|
```
|
|
142
150
|
|
|
143
|
-
### UMD
|
|
151
|
+
### UMD Usage (Script Tag)
|
|
144
152
|
|
|
145
|
-
>
|
|
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
|
-
|
|
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
|
-
<!--
|
|
164
|
+
<!-- Include TencentCloudChat SDK (Professional Edition) -->
|
|
157
165
|
<script src="https://unpkg.com/@tencentcloud/lite-chat/professional.js"></script>
|
|
158
166
|
|
|
159
|
-
<!--
|
|
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
|
-
//
|
|
171
|
+
// Get SDK instance
|
|
164
172
|
const webPush = WebPushSDK.webPush;
|
|
165
173
|
|
|
166
|
-
//
|
|
167
|
-
const SDKAppID = 0; //
|
|
168
|
-
const appKey = '
|
|
169
|
-
const userID = '
|
|
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('
|
|
183
|
+
console.log('Registration successful:', registrationID);
|
|
176
184
|
})
|
|
177
185
|
.catch((error) => {
|
|
178
|
-
console.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('
|
|
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('
|
|
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 -
|
|
218
|
-
* 2 -
|
|
219
|
-
* 3 -
|
|
220
|
-
* 4 -
|
|
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`:
|
|
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
|
|
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
|
|
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.
|
|
2
|
+
const version = "1.0.4";
|
|
3
3
|
var Placement = /* @__PURE__ */ ((Placement2) => {
|
|
4
4
|
Placement2[Placement2["CENTER"] = 0] = "CENTER";
|
|
5
5
|
Placement2[Placement2["TOP_LEFT"] = 1] = "TOP_LEFT";
|