tpns_flutter_plugin.dart
16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import 'dart:async';
import 'package:flutter/services.dart';
import 'dart:io';
import 'package:tpns_flutter_plugin/android/xg_android_api.dart';
/// 设备token绑定的类型,绑定指定类型之后,就可以在信鸽前端按照指定的类型进行指定范围的推送
/// none:当前设备token不绑定任何类型,可以使用token单推,或者是全量推送(3.2.0+ 不推荐使用 )
/// account:当前设备token与账号绑定之后,可以使用账号推送
/// tag:当前设备token与指定标签绑定之后,可以使用标签推送
enum XGBindType { none, account, tag }
/// UNKNOWN 默认类型,当前只支持此类型的推送
/// CUSTOM 自定义
/// IDFA 广告唯一标识,iOS 专用,安卓侧默认为UNKNOWN类型
/// PHONE_NUMBER 手机号码
/// WX_OPEN_ID 微信 OPENID
/// QQ_OPEN_ID QQ OPENID
/// EMAIL 邮箱
/// SINA_WEIBO 新浪微博
/// ALIPAY 支付宝
/// TAOBAO 淘宝
/// DOUBAN 豆瓣
/// BAIDU 百度
/// JINGDONG 京东
/// IMEI 安卓手机标识,安卓专用,iOS默认为UNKNOWN类型
enum AccountType {
UNKNOWN,
CUSTOM,
IDFA,
PHONE_NUMBER,
WX_OPEN_ID,
QQ_OPEN_ID,
EMAIL,
SINA_WEIBO,
ALIPAY,
TAOBAO,
DOUBAN,
FACEBOOK,
TWITTER,
GOOGLE,
BAIDU,
JINGDONG,
LINKEDIN,
IMEI
}
typedef Future<dynamic> EventHandler(String res);
typedef Future<dynamic> EventHandlerMap(Map<String, dynamic> event);
/* ======TPNS推送接口类====== */
class XgFlutterPlugin {
static const MethodChannel _channel =
const MethodChannel('tpns_flutter_plugin');
static XgAndroidApi xgApi = new XgAndroidApi(_channel);
/// 注册推送服务失败回调
EventHandler? _onRegisteredDeviceToken;
/// 注册推送服务成功回调
EventHandler? _onRegisteredDone;
/// 注销推送服务回调
EventHandler? _unRegistered;
/// 前台收到通知消息回调
EventHandlerMap? _onReceiveNotificationResponse;
/// 收到透传、静默消息回调
EventHandlerMap? _onReceiveMessage;
/// 通知点击回调
EventHandlerMap? _xgPushClickAction;
/// TPNS长链接建立回调,仅iOS
EventHandler? _xgPushNetworkConnected;
/// 设置角标回调仅iOS
EventHandler? _xgPushDidSetBadge;
/// 绑定账号/标签/用户属性回调,V1.2.8开始返回类型由String->Map,新增code及type标识!
EventHandlerMap? _xgPushDidBindWithIdentifier;
/// 解绑账号/标签/用户属性回调,V1.2.8开始返回类型由String->Map,新增code及type标识!
EventHandlerMap? _xgPushDidUnbindWithIdentifier;
/// 更新标签/用户属性回调,V1.2.8开始返回类型由String->Map,新增code及type标识!
EventHandlerMap? _xgPushDidUpdatedBindedIdentifier;
/// 清除所有账号/标签/用户属性回调,V1.2.8开始返回类型由String->Map,新增code及type标识!
EventHandlerMap? _xgPushDidClearAllIdentifiers;
/// 获取sdk版本号
static Future<String?> get xgSdkVersion async {
final String? version = await _channel.invokeMethod('xgSdkVersion');
return version;
}
/// 获取信鸽token
static Future<String?> get xgToken async {
final String? xgToken = await _channel.invokeMethod('xgToken');
return xgToken;
}
/// 获取厂商Token,区别于tpns token
static Future<String?> get otherPushToken async {
final String? otherPushToken =
await _channel.invokeMethod('getOtherPushToken');
return otherPushToken;
}
/// 获取安卓厂商品牌,当前仅对安卓有效
static Future<String?> get otherPushType async {
if (Platform.isAndroid) {
final String? otherPushType =
await _channel.invokeMethod('getOtherPushType');
return otherPushType;
}
}
/// 集群域名配置(非广州集群需要在startXg之前调用此函数)
void configureClusterDomainName(String domainStr) {
if (Platform.isIOS) {
_channel.invokeMethod('configureClusterDomainName',
<String, dynamic>{'domainStr': domainStr});
} else {
xgApi.setServerSuffix(serverAddr: domainStr);
}
}
/* ======信鸽注册反注册和debug接口====== */
/// 注册推送服务
/// iOS需传accessId和accessKey; withInAppAlert为可选参数,默认为true。
/// android不需要传参数
void startXg(String accessId, String accessKey,
{bool withInAppAlert = true}) {
if (Platform.isIOS) {
_channel.invokeMethod('startXg', <String, dynamic>{
'accessId': accessId,
'accessKey': accessKey,
'withInAppAlert': withInAppAlert,
});
} else if (Platform.isAndroid) {
xgApi.setAccessKey(accessKey: accessKey);
xgApi.setAccessId(accessId: accessId);
xgApi.regPush();
} else if (Platform.isOhos) {
_channel.invokeMethod('startXg', <String, dynamic>{
'accessId': accessId,
'accessKey': accessKey,
'withInAppAlert': withInAppAlert,
});
}
}
/// 注销推送服务(注销后无法再收到任何推送)
void stopXg() {
_channel.invokeMethod('stopXg');
}
/// debug模式
void setEnableDebug(bool enableDebug) {
_channel.invokeMethod(
'setEnableDebug', <String, dynamic>{'enableDebug': enableDebug});
}
/* ============账号接口V1.0.4新增============ */
/// 设置账号
/// account 账号标识
/// accountType 账号类型枚举
void setAccount(String account, AccountType accountType) {
String _accountType = accountType.toString().split('.').last;
if (Platform.isIOS) {
_channel.invokeMethod('setAccount',
<String, dynamic>{'account': account, 'accountType': _accountType});
} else {
xgApi.bindAccountWithType(account: account, accountType: _accountType);
}
}
/// 删除指定账号
/// account 账号标识
/// accountType 账号类型枚举
void deleteAccount(String account, AccountType accountType) {
String _accountType = accountType.toString().split('.').last;
if (Platform.isIOS) {
_channel.invokeMethod('deleteAccount',
<String, dynamic>{'account': account, 'accountType': _accountType});
} else {
xgApi.delAccountWithType(account: account, accountType: _accountType);
}
}
/// 删除所有账号
void cleanAccounts() {
if (Platform.isIOS) {
_channel.invokeMethod('cleanAccounts');
} else {
xgApi.delAllAccount();
}
}
/* ============标签接口V1.0.4新增============ */
/// 追加标签
/// tags类型为字符串数组(标签字符串不允许有空格或者是tab字符) [tagStr]
void addTags(List<String> tags) {
if (Platform.isIOS) {
_channel.invokeMethod('addTags', tags);
} else {
xgApi.addXgTags(tagNames: tags);
}
}
/// 覆盖标签(清除所有标签再追加)
/// tags类型为字符串数组(标签字符串不允许有空格或者是tab字符) [tagStr]
void setTags(List<String> tags) {
if (Platform.isIOS) {
_channel.invokeMethod('setTags', tags);
} else {
xgApi.setXgTags(tagNames: tags);
}
}
/// 删除指定标签
/// tags类型为字符串数组(标签字符串不允许有空格或者是tab字符) [tagStr]
void deleteTags(List<String> tags) {
if (Platform.isIOS) {
_channel.invokeMethod('deleteTags', tags);
} else {
xgApi.deleteXgTags(tagNames: tags);
}
}
/// 清除所有标签
void cleanTags() {
if (Platform.isIOS) {
_channel.invokeMethod('cleanTags');
} else {
xgApi.cleanXgTags();
}
}
/* ============用户属性接口V1.1.5新增============ */
/// 新增用户属性
/// attributes 类型为 Map 字典(k, v字符串不允许有空格或者是tab字符) {'key': 'value'}
void upsertAttributes(Map<String, String> attributes) {
if (Platform.isIOS) {
_channel.invokeMethod('upsertAttributes', attributes);
} else {
xgApi.upsertAttributes(attributes: attributes);
}
}
/// 删除用户属性
/// attributes 类型为字符串数组(字符串不允许有空格或者是tab字符) ['key']
void delAttributes(List<String> attributes) {
if (Platform.isIOS) {
_channel.invokeMethod('delAttributes', attributes);
} else {
xgApi.delAttributes(attributes: attributes);
}
}
/// 更新用户属性
/// attributes 类型为 Map 字典(k, v字符串不允许有空格或者是tab字符) {'key': 'value'}
void clearAndAppendAttributes(Map<String, String> attributes) {
if (Platform.isIOS) {
_channel.invokeMethod('clearAndAppendAttributes', attributes);
} else {
xgApi.clearAndAppendAttributes(attributes: attributes);
}
}
/// 清除全部用户属性
void clearAttributes() {
if (Platform.isIOS) {
_channel.invokeMethod('clearAttributes');
} else {
xgApi.clearAttributes();
}
}
/* ======角标====== */
/// 同步角标值到TPNS服务器
void setBadge(int badgeSum) {
if (Platform.isIOS) {
_channel
.invokeMethod('setBadge', <String, dynamic>{'badgeSum': badgeSum});
}
}
void clearBadge(int badgeSum) {
if (Platform.isOhos) {
_channel.invokeMethod('clearBadge');
}
}
/// 设置应用角标(当同步角标值到TPNS成功后,用户设置应用角标为对应的值)
void setAppBadge(int badgeSum) {
if (Platform.isIOS) {
_channel
.invokeMethod('setAppBadge', <String, dynamic>{'badgeSum': badgeSum});
}
}
/* ======获取信鸽AndroidApi====== */
/// 获取XgAndroidApi
XgAndroidApi getXgAndroidApi() {
return xgApi;
}
/*****************************************账号和标签废弃接口,V1.0.4废弃请使用新接口*********************************/
/* ======单个操作账号和标签====== */
/// 绑定账号或标签
void bindWithIdentifier({
required String identify,
required XGBindType bindType,
}) {
if (Platform.isIOS) {
_channel.invokeMethod('bindWithIdentifier',
{'identify': identify, 'bindType': bindType.index});
} else {
if (bindType.index == XGBindType.tag.index) {
xgApi.addXgTags(tagNames: [identify]);
} else if (bindType.index == XGBindType.account.index) {
xgApi.appendAccount(account: identify);
}
}
}
/// 更新账号或标签
void updateBindIdentifier({
required String identify,
required XGBindType bindType,
}) {
if (Platform.isIOS) {
_channel.invokeMethod('updateBindIdentifier',
{'identify': identify, 'bindType': bindType.index});
} else {
if (bindType.index == XGBindType.tag.index) {
xgApi.setXgTag(tagName: identify);
} else if (bindType.index == XGBindType.account.index) {
xgApi.bindAccount(account: identify);
}
}
}
/// 解绑账号或标签
void unbindWithIdentifier({
required String identify,
required XGBindType bindType,
}) {
if (Platform.isIOS) {
_channel.invokeMethod('unbindWithIdentifier',
{'identify': identify, 'bindType': bindType.index});
} else {
if (bindType.index == XGBindType.tag.index) {
xgApi.deleteXgTag(tagName: identify);
} else if (bindType.index == XGBindType.account.index) {
xgApi.delAccount(account: identify);
}
}
}
/// 批量绑定账号或标签
/// ios 对于账号操作. List类型为dic数组[{'account':identifyStr, 'accountType':0}]
/// ios 对于标签操作,List类型为字符串数组(标签字符串不允许有空格或者是tab字符) [identifyStr]
/// android List类型为字符串数组(标签字符串不允许有空格或者是tab字符) [identifyStr]
void bindWithIdentifiers({
required List identifys,
required XGBindType bindType,
}) {
if (Platform.isIOS) {
_channel.invokeMethod('bindWithIdentifiers',
{'identifys': identifys, 'bindType': bindType.index});
} else {
if (bindType.index == XGBindType.tag.index) {
xgApi.addXgTags(tagNames: identifys);
}
}
}
/// 批量更新账号或标签
/// ios 对于账号操作. List类型为dic数组[{'account':identifyStr, 'accountType':0}]
/// ios 对于标签操作,List类型为字符串数组(标签字符串不允许有空格或者是tab字符) [identifyStr]
/// android List类型为字符串数组(标签字符串不允许有空格或者是tab字符) [identifyStr]
void updateBindIdentifiers({
required List identifys,
required XGBindType bindType,
}) {
if (Platform.isIOS) {
_channel.invokeMethod('updateBindIdentifiers',
{'identifys': identifys, 'bindType': bindType.index});
} else {
if (bindType.index == XGBindType.tag.index) {
xgApi.setXgTags(tagNames: identifys);
}
}
}
/// 批量解绑账号或标签
/// ios 对于账号操作. List类型为dic数组[{'account':identifyStr, 'accountType':0}]
/// ios 对于标签操作,List类型为字符串数组(标签字符串不允许有空格或者是tab字符) [identifyStr]
/// android List类型为字符串数组(标签字符串不允许有空格或者是tab字符) [identifyStr]
void unbindWithIdentifiers({
required List identifys,
required XGBindType bindType,
}) {
if (Platform.isIOS) {
_channel.invokeMethod('unbindWithIdentifiers',
{'identifys': identifys, 'bindType': bindType.index});
} else {
if (bindType.index == XGBindType.tag.index) {
xgApi.deleteXgTags(tagNames: identifys);
}
}
}
/// 清除全部账号或标签
void clearAllIdentifier(XGBindType bindType) {
if (Platform.isIOS) {
_channel.invokeMethod(
'clearAllIdentifier', <String, dynamic>{'bindType': bindType.index});
} else {
if (bindType.index == XGBindType.tag.index) {
xgApi.cleanXgTags();
} else if (bindType.index == XGBindType.account.index) {
xgApi.delAllAccount();
}
}
}
//*******************************************请不要再使用以上账号和标签接口****************************************************/
/* ======信鸽callback====== */
void addEventHandler({
EventHandler? onRegisteredDeviceToken,
/// TPNS注册失败回调
EventHandler? onRegisteredDone,
/// TPNS注册成功回调
EventHandler? unRegistered,
EventHandlerMap? onReceiveNotificationResponse,
EventHandlerMap? onReceiveMessage,
EventHandler? xgPushNetworkConnected,
EventHandler? xgPushDidSetBadge,
EventHandlerMap? xgPushDidBindWithIdentifier,
EventHandlerMap? xgPushDidUnbindWithIdentifier,
EventHandlerMap? xgPushDidUpdatedBindedIdentifier,
EventHandlerMap? xgPushDidClearAllIdentifiers,
EventHandlerMap? xgPushClickAction,
}) {
_onRegisteredDeviceToken = onRegisteredDeviceToken;
_onRegisteredDone = onRegisteredDone;
_unRegistered = unRegistered;
_onReceiveNotificationResponse = onReceiveNotificationResponse;
_onReceiveMessage = onReceiveMessage;
_xgPushNetworkConnected = xgPushNetworkConnected;
_xgPushDidSetBadge = xgPushDidSetBadge;
_xgPushDidBindWithIdentifier = xgPushDidBindWithIdentifier;
_xgPushDidUnbindWithIdentifier = xgPushDidUnbindWithIdentifier;
_xgPushDidUpdatedBindedIdentifier = xgPushDidUpdatedBindedIdentifier;
_xgPushDidClearAllIdentifiers = xgPushDidClearAllIdentifiers;
_xgPushClickAction = xgPushClickAction;
_channel.setMethodCallHandler(_handleMethod);
}
Future<dynamic> _handleMethod(MethodCall call) async {
switch (call.method) {
case "onRegisteredDeviceToken":
return _onRegisteredDeviceToken!(call.arguments);
case "onRegisteredDone":
return _onRegisteredDone!(call.arguments);
case "unRegistered":
return _unRegistered!(call.arguments);
case "onReceiveNotificationResponse":
return _onReceiveNotificationResponse!(
call.arguments.cast<String, dynamic>());
case "onReceiveMessage":
return _onReceiveMessage!(call.arguments.cast<String, dynamic>());
case "xgPushNetworkConnected":
return _xgPushNetworkConnected!(call.arguments);
case "xgPushDidSetBadge":
return _xgPushDidSetBadge!(call.arguments);
case "xgPushDidBindWithIdentifier":
return _xgPushDidBindWithIdentifier!(
call.arguments.cast<String, dynamic>());
case "xgPushDidUnbindWithIdentifier":
return _xgPushDidUnbindWithIdentifier!(
call.arguments.cast<String, dynamic>());
case "xgPushDidUpdatedBindedIdentifier":
return _xgPushDidUpdatedBindedIdentifier!(
call.arguments.cast<String, dynamic>());
case "xgPushDidClearAllIdentifiers":
return _xgPushDidClearAllIdentifiers!(
call.arguments.cast<String, dynamic>());
case "xgPushClickAction":
return _xgPushClickAction!(call.arguments.cast<String, dynamic>());
default:
throw new UnsupportedError("Unrecongnized Event");
}
}
}