XgFlutterPlugin.ets
5.09 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
import {
FlutterPlugin,
FlutterPluginBinding,
MethodCall,
MethodCallHandler,
MethodChannel,
MethodResult,
AbilityAware,
AbilityPluginBinding,
FlutterAbility,
NewWantListener,
} from '@ohos/flutter_ohos';
import TPushHelper from './helper/TPushHelper';
import { UIAbility,AbilityConstant, common, Want } from '@kit.AbilityKit';
import Log from '@ohos/flutter_ohos/src/main/ets/util/Log';
import { CacheManager } from './helper/CacheManager';
import { TPush } from 'tpns';
import hilog from '@ohos.hilog';
const TAG: string = "TPushOhos";
/** XgFlutterPlugin **/
export default class XgFlutterPlugin implements FlutterPlugin, MethodCallHandler,AbilityAware,NewWantListener{
private channel: MethodChannel | null = null;
private applicationContext: Context | null = null;
private ability: UIAbility | null = null;
private binding: AbilityPluginBinding | null = null
constructor() {
}
getUniqueClassName(): string {
return "XgFlutterPlugin"
}
onAttachedToAbility(binding: AbilityPluginBinding): void {
Log.i(TAG, "onAttachedToAbility");
this.ability = binding.getAbility()
this.binding = binding
binding.addOnNewWantListener(this)
}
onDetachedFromAbility(): void {
Log.i(TAG, "onDetachedFromAbility");
this.ability = null;
this.binding?.removeOnNewWantListener(this)
this.binding = 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)
}
}
onNewWant(want: Want, launchParams: AbilityConstant.LaunchParam): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onNewWant' + JSON.stringify(want));
if (want && want.parameters) {
try {
// want判断消息是否是腾讯云推送的消息
const notificationMessage = TPushHelper.parseNotificationMessage(want);
if(notificationMessage){
const data = want.parameters;
if (data != null) {
hilog.info(0x0000, 'testTag', '%{public}s', "onNewWant" + JSON.stringify(data));
// 或者使用 Object.keys 方法遍历
Object.keys(data).forEach(key => {
if (key.toString() === 'tpnsChanel') {
// 使用 EventSink 发送数据后,dart断的事件监听回调会收到发送的数据。
const value = data[key];
console.log(`Key: ${key}, Value: ${typeof value === 'object' ? JSON.stringify(value) : value}`);
this.channel?.invokeMethod("getIntent", value)
}
});
} else {
hilog.info(0x0000, 'testTag', 'EntryAbility data was null');
}
}
} catch (err) {
hilog.error(0x0000, 'testTag', 'TPushHelper parseNotificationMessage exception', err);
}
} else {
hilog.info(0x0000, 'testTag', 'EntryAbility want or want.parameters was 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()
}
}
}