@tencentcloud/web-push 1.0.2 → 1.0.3
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 +102 -175
- package/dist/index.d.ts +259 -0
- package/{index.esm.js → dist/index.esm.js} +1118 -158
- package/dist/index.umd.js +1 -0
- package/dist/src/components/message-popup.d.ts +63 -0
- package/{src → dist/src}/core/web-push-sdk.d.ts +23 -1
- package/{src → dist/src}/index.d.ts +1 -0
- package/{src → dist/src}/types/inner.d.ts +2 -10
- package/dist/src/types/outer.d.ts +120 -0
- package/{src → dist/src}/utils/logger.d.ts +6 -0
- package/{src → dist/src}/utils/validator.d.ts +0 -13
- package/dist/sw.js +1 -0
- package/package.json +47 -9
- package/src/__tests__/index.test.ts +120 -0
- package/src/__tests__/integration.test.ts +285 -0
- package/src/__tests__/setup.ts +210 -0
- package/src/__tests__/types.test.ts +303 -0
- package/src/__tests__/web-push-sdk.test.ts +257 -0
- package/src/components/message-popup.ts +1007 -0
- package/src/core/event-emitter.ts +61 -0
- package/src/core/service-worker-manager.ts +614 -0
- package/src/core/web-push-sdk.ts +690 -0
- package/src/debug/GenerateTestUserSig.js +37 -0
- package/src/debug/index.d.ts +6 -0
- package/src/debug/index.js +1 -0
- package/src/debug/lib-generate-test-usersig-es.min.js +2 -0
- package/src/index.ts +9 -0
- package/src/service-worker/sw.ts +494 -0
- package/src/types/index.ts +2 -0
- package/src/types/inner.ts +44 -0
- package/src/types/outer.ts +142 -0
- package/src/utils/browser-support.ts +412 -0
- package/src/utils/logger.ts +66 -0
- package/src/utils/storage.ts +51 -0
- package/src/utils/validator.ts +267 -0
- package/CHANGELOG.md +0 -55
- package/index.d.ts +0 -110
- package/index.umd.js +0 -1
- package/src/types/outer.d.ts +0 -66
- package/sw.js +0 -1
- /package/{src → dist/src}/core/event-emitter.d.ts +0 -0
- /package/{src → dist/src}/core/service-worker-manager.d.ts +0 -0
- /package/{src → dist/src}/service-worker/sw.d.ts +0 -0
- /package/{src → dist/src}/types/index.d.ts +0 -0
- /package/{src → dist/src}/utils/browser-support.d.ts +0 -0
- /package/{src → dist/src}/utils/storage.d.ts +0 -0
package/README.md
CHANGED
|
@@ -1,91 +1,75 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Web Push SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
基于 Web 技术栈的推送服务 SDK,核心使用 Service Worker、Push API 和 Notification API 实现网页推送功能。
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## 特性
|
|
6
6
|
|
|
7
|
-
- 🚀
|
|
8
|
-
- 📱
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
7
|
+
- 🚀 基于现代 Web 标准 API
|
|
8
|
+
- 📱 支持跨平台推送通知
|
|
9
|
+
- 💬 **在线推送弹窗**:应用内消息实时弹窗显示
|
|
10
|
+
- 🎨 **自定义弹窗样式**:支持位置、样式、行为全方位自定义
|
|
11
|
+
- 🔧 TypeScript 支持
|
|
12
|
+
- 📊 内置数据统计功能
|
|
13
|
+
- 🎯 事件驱动架构
|
|
14
|
+
- 💾 本地状态持久化
|
|
15
|
+
- 🔒 安全的 VAPID 认证
|
|
14
16
|
|
|
15
|
-
##
|
|
17
|
+
## 浏览器支持
|
|
16
18
|
|
|
17
19
|
- Chrome 50+
|
|
18
20
|
- Firefox 44+
|
|
19
21
|
- Safari 16+
|
|
20
22
|
- Edge 17+
|
|
21
23
|
|
|
22
|
-
##
|
|
24
|
+
## 安装
|
|
23
25
|
|
|
24
|
-
###
|
|
25
|
-
|
|
26
|
-
**Option 1: NPM Installation**
|
|
27
|
-
|
|
28
|
-
```bash
|
|
29
|
-
npm install @tencentcloud/web-push --save
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
or
|
|
26
|
+
### 方式一:NPM 安装
|
|
33
27
|
|
|
34
28
|
```bash
|
|
35
|
-
|
|
29
|
+
npm install @tencentcloud/web-push
|
|
36
30
|
```
|
|
37
31
|
|
|
38
|
-
|
|
32
|
+
### 方式二:CDN 引入(UMD)
|
|
39
33
|
|
|
40
|
-
|
|
34
|
+
通过 `<script>` 标签直接引入使用:
|
|
41
35
|
|
|
42
36
|
```html
|
|
43
|
-
<!--
|
|
37
|
+
<!-- 引入 TencentCloudChat SDK (专业版) -->
|
|
44
38
|
<script src="https://unpkg.com/@tencentcloud/lite-chat/professional.js"></script>
|
|
45
39
|
|
|
46
|
-
<!--
|
|
40
|
+
<!-- 引入 Web Push SDK -->
|
|
47
41
|
<script src="https://unpkg.com/@tencentcloud/web-push/index.umd.js"></script>
|
|
48
42
|
|
|
49
43
|
<script>
|
|
50
|
-
// SDK
|
|
44
|
+
// SDK 会自动挂载到全局变量 WebPushSDK
|
|
51
45
|
const webPush = WebPushSDK.webPush;
|
|
52
46
|
|
|
53
|
-
//
|
|
47
|
+
// 使用方式与 ES6 模块相同
|
|
54
48
|
webPush.registerPush({
|
|
55
|
-
SDKAppID:
|
|
56
|
-
appKey: '
|
|
57
|
-
userID: '
|
|
49
|
+
SDKAppID: 0,
|
|
50
|
+
appKey: '',
|
|
51
|
+
userID: '',
|
|
58
52
|
});
|
|
59
53
|
</script>
|
|
60
54
|
```
|
|
61
55
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
```html
|
|
65
|
-
<!-- Include Chat SDK -->
|
|
66
|
-
<script src="./path/to/chat-sdk.js"></script>
|
|
67
|
-
|
|
68
|
-
<!-- Include Web Push SDK -->
|
|
69
|
-
<script src="./dist/index.umd.js"></script>
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
### Step 2: Configure the Service Worker File
|
|
56
|
+
### 步骤2:配置 Service Worker 文件
|
|
73
57
|
|
|
74
|
-
|
|
58
|
+
集成 `@tencentcloud/web-push` 后,需要将 **Service Worker(sw.js)**复制到您项目**的根目录**。网站部署后,确保该文件可以通过 `https://your-domain.com/sw.js` 的路径被访问到。否则浏览器将无法注册 **Service Worker**。
|
|
75
59
|
|
|
76
|
-
>
|
|
60
|
+
> **说明:**
|
|
77
61
|
>
|
|
78
|
-
> -
|
|
79
|
-
> -
|
|
62
|
+
> - sw.js 文件**必须放在网站的根目录**才能正常工作,这是浏览器的安全限制。
|
|
63
|
+
> - sw.js 只有在 **HTTPS 连接(或本地开发环境 localhost)**下才能被注册成功。请确保您的生产环境网站支持 **HTTPS**。
|
|
80
64
|
|
|
81
|
-
**
|
|
65
|
+
**public 目录的作用**:在现代前端项目(如 Vue, React, Next.js 等)中,**public 目录是一个特殊目录,它的内容在构建时不会被编译或改名**。放置在 public 目录的文件会原封不动地被复制到网站的根目录。
|
|
82
66
|
|
|
83
|
-
>
|
|
67
|
+
> **注意:**
|
|
84
68
|
>
|
|
85
|
-
> -
|
|
86
|
-
> -
|
|
69
|
+
> - 将 sw.js 放在 src 或其他目录,它可能会被打包工具(Webpack/Vite)编译、改名(例如变成 sw.123abcde.js),从而无法被正确注册。
|
|
70
|
+
> - 如果您的项目**没有 public 目录**(例如老式 HTML 项目),请直接将 **sw.js** 放在 **index.html** 所在的同级目录下,确保构建输出后位于根目录即可。
|
|
87
71
|
|
|
88
|
-
【
|
|
72
|
+
【MacOS】
|
|
89
73
|
|
|
90
74
|
```bash
|
|
91
75
|
cp node_modules/@tencentcloud/web-push/dist/sw.js public/sw.js
|
|
@@ -97,17 +81,17 @@ cp node_modules/@tencentcloud/web-push/dist/sw.js public/sw.js
|
|
|
97
81
|
copy node_modules\@tencentcloud\web-push\dist\sw.js public\sw.js
|
|
98
82
|
```
|
|
99
83
|
|
|
100
|
-
|
|
84
|
+
## 快速开始
|
|
101
85
|
|
|
102
|
-
|
|
86
|
+
在您的主页面(例如:`index.js`)中,引入 `@tencentcloud/web-push` 并注册。
|
|
103
87
|
|
|
104
88
|
<table>
|
|
105
89
|
<tr>
|
|
106
|
-
<td rowspan="1" colSpan="1"
|
|
90
|
+
<td rowspan="1" colSpan="1" >参数</td>
|
|
107
91
|
|
|
108
|
-
<td rowspan="1" colSpan="1"
|
|
92
|
+
<td rowspan="1" colSpan="1" >类型</td>
|
|
109
93
|
|
|
110
|
-
<td rowspan="1" colSpan="1"
|
|
94
|
+
<td rowspan="1" colSpan="1" >说明</td>
|
|
111
95
|
</tr>
|
|
112
96
|
|
|
113
97
|
<tr>
|
|
@@ -115,7 +99,7 @@ In your homepage (for example: `index.js`), add `@tencentcloud/web-push` and reg
|
|
|
115
99
|
|
|
116
100
|
<td rowspan="1" colSpan="1" >Number</td>
|
|
117
101
|
|
|
118
|
-
<td rowspan="1" colSpan="1"
|
|
102
|
+
<td rowspan="1" colSpan="1" >推送服务 Push 的 SDKAppID。</td>
|
|
119
103
|
</tr>
|
|
120
104
|
|
|
121
105
|
<tr>
|
|
@@ -123,7 +107,7 @@ In your homepage (for example: `index.js`), add `@tencentcloud/web-push` and reg
|
|
|
123
107
|
|
|
124
108
|
<td rowspan="1" colSpan="1" >String</td>
|
|
125
109
|
|
|
126
|
-
<td rowspan="1" colSpan="1"
|
|
110
|
+
<td rowspan="1" colSpan="1" >推送服务 Push 的客户端密钥。</td>
|
|
127
111
|
</tr>
|
|
128
112
|
|
|
129
113
|
<tr>
|
|
@@ -131,36 +115,36 @@ In your homepage (for example: `index.js`), add `@tencentcloud/web-push` and reg
|
|
|
131
115
|
|
|
132
116
|
<td rowspan="1" colSpan="1" >String</td>
|
|
133
117
|
|
|
134
|
-
<td rowspan="1" colSpan="1"
|
|
118
|
+
<td rowspan="1" colSpan="1" >注册推送用户的 userID。用户的唯一标识符,由您定义,只能包含大小写字母(a-z,A-Z)、数字(0-9)、下划线和连字符。</td>
|
|
135
119
|
</tr>
|
|
136
120
|
</table>
|
|
137
121
|
|
|
138
122
|
```javascript
|
|
139
|
-
import
|
|
123
|
+
import webPush from '@tencentcloud/web-push';
|
|
140
124
|
|
|
141
|
-
const SDKAppID = 0; //
|
|
142
|
-
const appKey = ''; //
|
|
143
|
-
const userID = ''; //
|
|
125
|
+
const SDKAppID = 0; // 您的 SDKAppID
|
|
126
|
+
const appKey = ''; // 客户端密钥
|
|
127
|
+
const userID = ''; // 用户 ID
|
|
144
128
|
|
|
145
|
-
//
|
|
146
|
-
await
|
|
129
|
+
// 注册推送服务
|
|
130
|
+
await webPush.registerPush({ SDKAppID, appKey, userID });
|
|
147
131
|
|
|
148
|
-
//
|
|
149
|
-
|
|
150
|
-
console.log('
|
|
132
|
+
// 监听推送消息
|
|
133
|
+
webPush.addPushListener(webPush.EVENT.MESSAGE_RECEIVED, (message) => {
|
|
134
|
+
console.log('收到推送消息:', message);
|
|
151
135
|
});
|
|
152
136
|
|
|
153
|
-
//
|
|
154
|
-
|
|
155
|
-
console.log('
|
|
137
|
+
// 监听通知点击
|
|
138
|
+
webPush.addPushListener(webPush.EVENT.NOTIFICATION_CLICKED, (data) => {
|
|
139
|
+
console.log('通知被点击:', data);
|
|
156
140
|
});
|
|
157
141
|
```
|
|
158
142
|
|
|
159
|
-
### UMD
|
|
143
|
+
### UMD 方式(Script 标签)
|
|
160
144
|
|
|
161
|
-
>
|
|
145
|
+
> **重要提示**: 使用 UMD 方式时,必须先引入 TencentCloudChat SDK (专业版),否则会出现 "Cannot read properties of undefined (reading 'create')" 错误。
|
|
162
146
|
|
|
163
|
-
|
|
147
|
+
如果您使用 UMD 方式引入,可以参考以下示例:
|
|
164
148
|
|
|
165
149
|
```html
|
|
166
150
|
<!DOCTYPE html>
|
|
@@ -169,65 +153,73 @@ If you're using UMD integration, here's an example:
|
|
|
169
153
|
<title>Web Push SDK UMD Example</title>
|
|
170
154
|
</head>
|
|
171
155
|
<body>
|
|
172
|
-
<!--
|
|
156
|
+
<!-- 引入 TencentCloudChat SDK (专业版) -->
|
|
173
157
|
<script src="https://unpkg.com/@tencentcloud/lite-chat/professional.js"></script>
|
|
174
158
|
|
|
175
|
-
<!--
|
|
159
|
+
<!-- 引入 Web Push SDK -->
|
|
176
160
|
<script src="https://unpkg.com/@tencentcloud/web-push/index.umd.js"></script>
|
|
177
161
|
|
|
178
162
|
<script>
|
|
179
|
-
//
|
|
163
|
+
// 获取 SDK 实例
|
|
180
164
|
const webPush = WebPushSDK.webPush;
|
|
181
165
|
|
|
182
|
-
//
|
|
183
|
-
const SDKAppID =
|
|
184
|
-
const appKey = '
|
|
185
|
-
const userID = '
|
|
166
|
+
// 配置参数
|
|
167
|
+
const SDKAppID = 0; // 您的 SDKAppID
|
|
168
|
+
const appKey = 'xxx'; // 客户端密钥
|
|
169
|
+
const userID = 'xxx'; // 用户 ID
|
|
186
170
|
|
|
187
|
-
//
|
|
171
|
+
// 注册推送服务
|
|
188
172
|
webPush
|
|
189
173
|
.registerPush({ SDKAppID, appKey, userID })
|
|
190
174
|
.then((registrationID) => {
|
|
191
|
-
console.log('
|
|
175
|
+
console.log('注册成功:', registrationID);
|
|
192
176
|
})
|
|
193
177
|
.catch((error) => {
|
|
194
|
-
console.error('
|
|
178
|
+
console.error('注册失败:', error);
|
|
195
179
|
});
|
|
196
180
|
|
|
197
|
-
//
|
|
181
|
+
// 监听推送消息
|
|
198
182
|
webPush.addPushListener(webPush.EVENT.MESSAGE_RECEIVED, (message) => {
|
|
199
|
-
console.log('
|
|
183
|
+
console.log('收到推送消息:', message);
|
|
200
184
|
});
|
|
201
185
|
|
|
202
|
-
//
|
|
186
|
+
// 监听通知点击
|
|
203
187
|
webPush.addPushListener(webPush.EVENT.NOTIFICATION_CLICKED, (data) => {
|
|
204
|
-
console.log('
|
|
188
|
+
console.log('通知被点击:', data);
|
|
205
189
|
});
|
|
206
190
|
</script>
|
|
207
191
|
</body>
|
|
208
192
|
</html>
|
|
209
193
|
```
|
|
210
194
|
|
|
211
|
-
## API
|
|
195
|
+
## API 文档
|
|
212
196
|
|
|
213
|
-
###
|
|
197
|
+
### 接口说明
|
|
214
198
|
|
|
215
|
-
| API |
|
|
216
|
-
| -------------------- | --------------------------------------------------- |
|
|
217
|
-
| `registerPush` | `options: RegisterPushOptions` |
|
|
218
|
-
| `unRegisterPush` | - |
|
|
219
|
-
| `addPushListener` | `eventName: EVENT, listener: Function<EventResult>` |
|
|
220
|
-
| `removePushListener` | `eventName: EVENT, listener: Function<EventResult>` |
|
|
199
|
+
| API | 参数 | 描述 |
|
|
200
|
+
| -------------------- | --------------------------------------------------- | ------------------ |
|
|
201
|
+
| `registerPush` | `options: RegisterPushOptions` | 注册推送服务 |
|
|
202
|
+
| `unRegisterPush` | - | 反注册关闭推送服务 |
|
|
203
|
+
| `addPushListener` | `eventName: EVENT, listener: Function<EventResult>` | 添加推送监听器 |
|
|
204
|
+
| `removePushListener` | `eventName: EVENT, listener: Function<EventResult>` | 移除推送监听器 |
|
|
221
205
|
|
|
222
|
-
###
|
|
206
|
+
### 类型定义
|
|
223
207
|
|
|
224
208
|
```typescript
|
|
225
209
|
interface RegisterPushOptions {
|
|
226
210
|
SDKAppID: number;
|
|
227
211
|
appKey: string;
|
|
228
212
|
userID: string;
|
|
229
|
-
serviceWorkerPath?: string;
|
|
230
213
|
chat?: any;
|
|
214
|
+
/**
|
|
215
|
+
* 日志级别:
|
|
216
|
+
* 0 - 普通级别,日志量较多,接入时建议使用
|
|
217
|
+
* 1 - release级别,SDK 输出关键信息,生产环境时建议使用(默认)
|
|
218
|
+
* 2 - 告警级别,SDK 只输出告警和错误级别的日志
|
|
219
|
+
* 3 - 错误级别,SDK 只输出错误级别的日志
|
|
220
|
+
* 4 - 无日志级别,SDK 将不打印任何日志
|
|
221
|
+
*/
|
|
222
|
+
logLevel?: LogLevel;
|
|
231
223
|
}
|
|
232
224
|
|
|
233
225
|
enum EVENT {
|
|
@@ -235,93 +227,28 @@ enum EVENT {
|
|
|
235
227
|
MESSAGE_REVOKED = 'message_revoked',
|
|
236
228
|
NOTIFICATION_CLICKED = 'notification_clicked',
|
|
237
229
|
}
|
|
238
|
-
|
|
239
|
-
interface Message {
|
|
240
|
-
ID: string;
|
|
241
|
-
type:
|
|
242
|
-
| 'TIMTextElem'
|
|
243
|
-
| 'TIMImageElem'
|
|
244
|
-
| 'TIMSoundElem'
|
|
245
|
-
| 'TIMVideoFileElem'
|
|
246
|
-
| 'TIMFileElem'
|
|
247
|
-
| 'TIMCustomElem'
|
|
248
|
-
| 'TIMRelayElem'
|
|
249
|
-
| 'TIMLocationElem'
|
|
250
|
-
| 'TIMFaceElem';
|
|
251
|
-
payload: any;
|
|
252
|
-
conversationID: string;
|
|
253
|
-
conversationType: 'C2C' | 'GROUP';
|
|
254
|
-
to: string;
|
|
255
|
-
from: string;
|
|
256
|
-
flow: string;
|
|
257
|
-
time: number;
|
|
258
|
-
status: string;
|
|
259
|
-
isRevoked: boolean;
|
|
260
|
-
nick: string;
|
|
261
|
-
avatar: string;
|
|
262
|
-
isPeerRead: boolean;
|
|
263
|
-
nameCard: string;
|
|
264
|
-
atUserList: string[];
|
|
265
|
-
cloudCustomData: string;
|
|
266
|
-
isDeleted: boolean;
|
|
267
|
-
isModified: boolean;
|
|
268
|
-
needReadReceipt: boolean;
|
|
269
|
-
readReceiptInfo: any;
|
|
270
|
-
isBroadcastMessage: boolean;
|
|
271
|
-
isSupportExtension: boolean;
|
|
272
|
-
receiverList?: string[];
|
|
273
|
-
revoker: string;
|
|
274
|
-
sequence: number;
|
|
275
|
-
progress: number;
|
|
276
|
-
revokerInfo: {
|
|
277
|
-
userID: string;
|
|
278
|
-
nick: string;
|
|
279
|
-
avatar: string;
|
|
280
|
-
};
|
|
281
|
-
revokeReason: string;
|
|
282
|
-
hasRiskContent: boolean;
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
interface MessageReceivedResult {
|
|
286
|
-
message: Message;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
interface MessageRevokedResult {
|
|
290
|
-
messageID: string;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
interface MessageNotificationClickedResult {
|
|
294
|
-
ext: string;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
interface EventResult {
|
|
298
|
-
data:
|
|
299
|
-
| MessageReceivedResult
|
|
300
|
-
| MessageRevokedResult
|
|
301
|
-
| MessageNotificationClickedResult;
|
|
302
|
-
}
|
|
303
230
|
```
|
|
304
231
|
|
|
305
|
-
###
|
|
232
|
+
### 事件类型
|
|
306
233
|
|
|
307
|
-
- `MESSAGE_RECEIVED`:
|
|
308
|
-
- `MESSAGE_REVOKED`:
|
|
309
|
-
- `NOTIFICATION_CLICKED`:
|
|
234
|
+
- `MESSAGE_RECEIVED`: 接收到推送消息(包括所有类型:standard、html、custom)
|
|
235
|
+
- `MESSAGE_REVOKED`: 消息被撤回
|
|
236
|
+
- `NOTIFICATION_CLICKED`: 通知被点击
|
|
310
237
|
|
|
311
|
-
##
|
|
238
|
+
## 注意事项
|
|
312
239
|
|
|
313
|
-
### Service Worker
|
|
240
|
+
### Service Worker 作用域
|
|
314
241
|
|
|
315
|
-
Service
|
|
242
|
+
Service Worker 必须放在网站根目录下(如 `/sw.js`),这样才能控制整个网站的推送消息。如果放在子目录下,将无法正常工作。
|
|
316
243
|
|
|
317
|
-
### HTTPS
|
|
244
|
+
### HTTPS 要求
|
|
318
245
|
|
|
319
|
-
|
|
246
|
+
Web Push API 要求网站必须使用 HTTPS 协议(本地开发环境的 localhost 除外)。
|
|
320
247
|
|
|
321
|
-
###
|
|
248
|
+
### 浏览器权限
|
|
322
249
|
|
|
323
|
-
|
|
250
|
+
用户首次使用时需要授权通知权限,请在合适的时机引导用户授权。
|
|
324
251
|
|
|
325
|
-
##
|
|
252
|
+
## 许可证
|
|
326
253
|
|
|
327
254
|
MIT License
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
export declare interface CustomMessage {
|
|
2
|
+
id: string;
|
|
3
|
+
MsgType: 'custom';
|
|
4
|
+
MsgContent: {
|
|
5
|
+
Data: string | object;
|
|
6
|
+
};
|
|
7
|
+
Ext?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export declare enum EVENT {
|
|
11
|
+
MESSAGE_RECEIVED = "message_received",
|
|
12
|
+
MESSAGE_REVOKED = "message_revoked",
|
|
13
|
+
NOTIFICATION_CLICKED = "notification_clicked",
|
|
14
|
+
CUSTOM_MESSAGE_RECEIVED = "custom_message_received"
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export declare interface EventResult {
|
|
18
|
+
data: MessageReceivedResult | MessageRevokedResult | MessageNotificationClickedResult;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export declare interface HtmlMessage {
|
|
22
|
+
id: string;
|
|
23
|
+
MsgType: 'html';
|
|
24
|
+
MsgContent: {
|
|
25
|
+
Placement?: Placement;
|
|
26
|
+
Duration?: number;
|
|
27
|
+
Html: string;
|
|
28
|
+
};
|
|
29
|
+
Ext?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export declare type LogLevel = 0 | 1 | 2 | 3 | 4;
|
|
33
|
+
|
|
34
|
+
export declare interface MessageNotificationClickedResult {
|
|
35
|
+
ext: String;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export declare class MessagePopup {
|
|
39
|
+
private container;
|
|
40
|
+
private popups;
|
|
41
|
+
private timers;
|
|
42
|
+
private messageHandlers;
|
|
43
|
+
private animationTimers;
|
|
44
|
+
private adjustHeightTimers;
|
|
45
|
+
private config;
|
|
46
|
+
private readonly defaultConfig;
|
|
47
|
+
constructor(config?: PopupConfig);
|
|
48
|
+
private init;
|
|
49
|
+
private createContainer;
|
|
50
|
+
private injectStyles;
|
|
51
|
+
show(message: StandardMessage | HtmlMessage): void;
|
|
52
|
+
private createStandardPopup;
|
|
53
|
+
private createHtmlPopup;
|
|
54
|
+
private addPopup;
|
|
55
|
+
private removeOldestPopup;
|
|
56
|
+
close(messageId: string): void;
|
|
57
|
+
/**
|
|
58
|
+
* 检查指定消息是否正在显示弹窗
|
|
59
|
+
* @param messageId 消息ID
|
|
60
|
+
* @returns 是否正在显示
|
|
61
|
+
*/
|
|
62
|
+
hasMessage(messageId: string): boolean;
|
|
63
|
+
closeAll(): void;
|
|
64
|
+
updateConfig(newConfig: Partial<PopupConfig>): void;
|
|
65
|
+
destroy(): void;
|
|
66
|
+
/**
|
|
67
|
+
* 根据 Placement 值应用容器位置
|
|
68
|
+
* 0-中间,1-左上,2-上中,3-右上,4-右中,5-右下,6-下中,7-左下,8-左中
|
|
69
|
+
*/
|
|
70
|
+
private applyPlacement;
|
|
71
|
+
/**
|
|
72
|
+
* 调整iframe高度以适应内容
|
|
73
|
+
*/
|
|
74
|
+
private adjustIframeHeight;
|
|
75
|
+
/**
|
|
76
|
+
* 创建iframe的完整HTML文档
|
|
77
|
+
*/
|
|
78
|
+
private createIframeHtml;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export declare interface MessageReceivedData {
|
|
82
|
+
content: string;
|
|
83
|
+
info: WebPushInfo;
|
|
84
|
+
extension: WebPushExt;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export declare interface MessageReceivedResult {
|
|
88
|
+
message: PushMessage | NotificationMessage;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export declare interface MessageRevokedResult {
|
|
92
|
+
messageID: String;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export declare interface NotificationMessage {
|
|
96
|
+
messageID: string;
|
|
97
|
+
title: string;
|
|
98
|
+
body: string;
|
|
99
|
+
icon?: string;
|
|
100
|
+
tag?: string;
|
|
101
|
+
data?: any;
|
|
102
|
+
timestamp: number;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export declare enum Placement {
|
|
106
|
+
CENTER = 0,// 中间
|
|
107
|
+
TOP_LEFT = 1,// 左上
|
|
108
|
+
TOP_CENTER = 2,// 上中
|
|
109
|
+
TOP_RIGHT = 3,// 右上
|
|
110
|
+
MIDDLE_RIGHT = 4,// 右中
|
|
111
|
+
BOTTOM_RIGHT = 5,// 右下
|
|
112
|
+
BOTTOM_CENTER = 6,// 下中
|
|
113
|
+
BOTTOM_LEFT = 7,// 左下
|
|
114
|
+
MIDDLE_LEFT = 8
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
declare interface PopupConfig {
|
|
118
|
+
/** 是否启用弹窗 */
|
|
119
|
+
enabled?: boolean;
|
|
120
|
+
/** 弹窗显示时长(毫秒),0 表示不自动关闭 */
|
|
121
|
+
duration?: number;
|
|
122
|
+
/** 弹窗位置 */
|
|
123
|
+
position?: 'center' | 'top-left' | 'top-center' | 'top-right' | 'right-center' | 'bottom-right' | 'bottom-center' | 'bottom-left' | 'left-center';
|
|
124
|
+
/** 最大显示数量 */
|
|
125
|
+
maxCount?: number;
|
|
126
|
+
/** 是否显示关闭按钮 */
|
|
127
|
+
showCloseButton?: boolean;
|
|
128
|
+
/** 是否允许点击弹窗 */
|
|
129
|
+
clickable?: boolean;
|
|
130
|
+
/** 自定义渲染函数 */
|
|
131
|
+
customRender?: (message: any, container: HTMLElement) => HTMLElement;
|
|
132
|
+
/** 点击回调 */
|
|
133
|
+
onClick?: (message: any, popup: HTMLElement) => void;
|
|
134
|
+
/** 关闭回调 */
|
|
135
|
+
onClose?: (message: any, popup: HTMLElement) => void;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export declare type PushMessage = StandardMessage | HtmlMessage | CustomMessage;
|
|
139
|
+
|
|
140
|
+
export declare interface RegisterPushOptions {
|
|
141
|
+
SDKAppID: number;
|
|
142
|
+
appKey: string;
|
|
143
|
+
userID: string;
|
|
144
|
+
serviceWorkerPath?: string;
|
|
145
|
+
chat?: any;
|
|
146
|
+
/**
|
|
147
|
+
* 日志级别:
|
|
148
|
+
* 0 - 普通级别,日志量较多,接入时建议使用
|
|
149
|
+
* 1 - release级别,SDK 输出关键信息,生产环境时建议使用(默认)
|
|
150
|
+
* 2 - 告警级别,SDK 只输出告警和错误级别的日志
|
|
151
|
+
* 3 - 错误级别,SDK 只输出错误级别的日志
|
|
152
|
+
* 4 - 无日志级别,SDK 将不打印任何日志
|
|
153
|
+
*/
|
|
154
|
+
logLevel?: LogLevel;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export declare interface StandardMessage {
|
|
158
|
+
id: string;
|
|
159
|
+
MsgType: 'standard';
|
|
160
|
+
MsgContent: {
|
|
161
|
+
Text: {
|
|
162
|
+
Title: string;
|
|
163
|
+
Desc: string;
|
|
164
|
+
};
|
|
165
|
+
Image?: {
|
|
166
|
+
URL: string;
|
|
167
|
+
LinkURL: string;
|
|
168
|
+
};
|
|
169
|
+
Close: 0 | 1;
|
|
170
|
+
Placement?: Placement;
|
|
171
|
+
Duration?: number;
|
|
172
|
+
Button?: Array<{
|
|
173
|
+
Text: string;
|
|
174
|
+
Icon?: string;
|
|
175
|
+
Url: string;
|
|
176
|
+
}>;
|
|
177
|
+
};
|
|
178
|
+
Ext?: string;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
declare const webPush: WebPushSDK_2;
|
|
182
|
+
export default webPush;
|
|
183
|
+
export { webPush }
|
|
184
|
+
|
|
185
|
+
export declare interface WebPushExt {
|
|
186
|
+
WebpushReportUrl: string;
|
|
187
|
+
OnlineClickExt: string;
|
|
188
|
+
TaskId: string;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export declare interface WebPushInfo {
|
|
192
|
+
Title: string;
|
|
193
|
+
Desc: string;
|
|
194
|
+
Icon: string;
|
|
195
|
+
Image: string;
|
|
196
|
+
URL: string;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export declare interface WebPushSDK {
|
|
200
|
+
registerPush(options?: RegisterPushOptions): Promise<any>;
|
|
201
|
+
unRegisterPush(): Promise<any>;
|
|
202
|
+
addPushListener(eventName: EVENT, listener: (data: EventResult) => void): any;
|
|
203
|
+
removePushListener(eventName: EVENT, listener: (data: EventResult) => void): any;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
declare class WebPushSDK_2 implements WebPushSDK {
|
|
207
|
+
static instance: WebPushSDK_2;
|
|
208
|
+
private eventEmitter;
|
|
209
|
+
private serviceWorkerManager;
|
|
210
|
+
private messagePopup;
|
|
211
|
+
private isRegistered;
|
|
212
|
+
private registrationID;
|
|
213
|
+
private chat;
|
|
214
|
+
private SDKAppID;
|
|
215
|
+
private appKey;
|
|
216
|
+
private vapidPublicKey;
|
|
217
|
+
private pendingMessages;
|
|
218
|
+
EVENT: typeof EVENT;
|
|
219
|
+
VERSION: string;
|
|
220
|
+
constructor();
|
|
221
|
+
static getInstance(): WebPushSDK_2;
|
|
222
|
+
registerPush(options?: RegisterPushOptions): Promise<string>;
|
|
223
|
+
unRegisterPush(): Promise<boolean>;
|
|
224
|
+
addPushListener(eventName: EVENT, listener: Function): string;
|
|
225
|
+
/**
|
|
226
|
+
* 内部方法:向 Service Worker 发送日志级别配置
|
|
227
|
+
* @param logLevel 日志级别 0-4
|
|
228
|
+
*/
|
|
229
|
+
private sendLogLevelToServiceWorker;
|
|
230
|
+
removePushListener(eventName: EVENT, listener: Function): boolean;
|
|
231
|
+
private checkBrowserSupport;
|
|
232
|
+
private requestNotificationPermission;
|
|
233
|
+
private pushLogin;
|
|
234
|
+
private onMessageReceived;
|
|
235
|
+
private onMessageRevoked;
|
|
236
|
+
private addChatListener;
|
|
237
|
+
private removeChatListener;
|
|
238
|
+
private getSystemPermissionMessage;
|
|
239
|
+
private showSystemPermissionAlert;
|
|
240
|
+
private getBrowserInstType;
|
|
241
|
+
private setToken;
|
|
242
|
+
private initializeBrowserCompatibility;
|
|
243
|
+
private setupVisibilityChangeListener;
|
|
244
|
+
private setupInternalListeners;
|
|
245
|
+
private pushStatistics;
|
|
246
|
+
private clearState;
|
|
247
|
+
/**
|
|
248
|
+
* 向 service-worker 发送通知消息,使用与 push 事件相同的数据格式
|
|
249
|
+
* @param webpushInfo webpush 信息,格式与 SW push 事件接收的数据一致
|
|
250
|
+
*/
|
|
251
|
+
private sendNotificationToServiceWorker;
|
|
252
|
+
/**
|
|
253
|
+
* 向 service-worker 发送消息撤回请求
|
|
254
|
+
* @param messageID 要撤回的消息ID
|
|
255
|
+
*/
|
|
256
|
+
private sendMessageRevokeToServiceWorker;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export { }
|