XgFlutterPlugin.ets
3.44 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
import {
FlutterPlugin,
FlutterPluginBinding,
MethodCall,
MethodCallHandler,
MethodChannel,
MethodResult,
} from '@ohos/flutter_ohos';
import TPushHelper from './helper/TPushHelper';
import { UIAbility, Want } from '@kit.AbilityKit';
import { AbilityAware, AbilityPluginBinding } from '@ohos/flutter_ohos';
import Log from '@ohos/flutter_ohos/src/main/ets/util/Log';
import { CacheManager } from './helper/CacheManager';
import { TPush } from 'tpns';
const TAG: string = "TPushOhos";
/** XgFlutterPlugin **/
export default class XgFlutterPlugin implements FlutterPlugin, MethodCallHandler {
private channel: MethodChannel | null = null;
private applicationContext: Context | null = null;
private ability: UIAbility | null = null;
constructor() {
}
getUniqueClassName(): string {
return "XgFlutterPlugin"
}
onAttachedToAbility(binding: AbilityPluginBinding): void {
Log.i(TAG, "onAttachedToAbility");
this.ability = binding.getAbility()
}
onDetachedFromAbility(): void {
Log.i(TAG, "onDetachedFromAbility");
this.ability = null;
}
onAttachedToEngine(binding: FlutterPluginBinding): void {
this.applicationContext = binding.getApplicationContext();
this.channel = new MethodChannel(binding.getBinaryMessenger(), "tpns_flutter_plugin");
this.channel.setMethodCallHandler(this);
CacheManager.init(this.applicationContext.getApplicationContext()); //初始化缓存功能
}
onDetachedFromEngine(binding: FlutterPluginBinding): void {
this.applicationContext = null;
if (this.channel != null) {
this.channel.setMethodCallHandler(null)
}
}
onMethodCall(call: MethodCall, result: MethodResult): void {
if (call.method == "getPlatformVersion") {
result.success("OpenHarmony ^ ^ ")
}else if (call.method == "xgToken") {
result.success(null)
}else if (call.method == "startXg") {
const accessId = call.argument("accessId") as string || ""
const accessKey = call.argument("accessKey") as string || ""
TPushHelper.init(this.applicationContext!,accessId,accessKey,this.channel,(err, strs) => {
if(err){
//注册失败
console.info(`test ${JSON.stringify(strs)}`)
result.error('000',strs,err)
}else{
// 注册成功
console.info(`test ${JSON.stringify(strs)}`)
TPushHelper.register((err, token) => {
if(token =='注册失败!'){
result.error('000','注册失败','注册失败')
}else{
this.channel?.invokeMethod("onRegisteredDone",token);
result.success(token)
}
});
}
});
}else if(call.method == "stopXg"){
TPushHelper.unregister((err, msg) => {
if(msg =="反注册失败"){
result.error('000','反注册失败','反注册失败')
}else{
this.channel?.invokeMethod("unRegistered",msg);
result.success(msg)
}
});
}else if(call.method == 'setAppBadge'){
const badgeSum = call.argument("badgeSum") as string || ""
TPushHelper.setBadge(Number(badgeSum),(err, msg) => {
if(msg == '设置应用角标失败'){
result.error('000','设置应用角标失败','设置应用角标失败')
}else{
result.success(msg)
}
});
}else if(call.method == 'resetBadgeNum'){
TPushHelper.clearBadge();
} else {
result.notImplemented()
}
}
}