EntryAbility.ets
5.53 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
import { FlutterAbility, FlutterEngine,FlutterPlugin, FlutterPluginBinding, EventChannel, EventSink, MethodChannel, MethodCall, MethodResult } from '@ohos/flutter_ohos';
import { GeneratedPluginRegistrant } from '../plugins/GeneratedPluginRegistrant';
import Want from '@ohos.app.ability.Want';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import hilog from '@ohos.hilog';
export default class EntryAbility extends FlutterAbility {
private TAG: string = "| com.naji.book |";
// 通道名称
public CHANNEL:string = "com.naji/book";
private channel: MethodChannel | null = null;
// private eventChannel?: EventChannel;
// private eventSink?: EventSink;
configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
GeneratedPluginRegistrant.registerWith(flutterEngine)
this.channel = new MethodChannel(flutterEngine.dartExecutor, this.CHANNEL);
// this.eventChannel = new EventChannel(flutterEngine.dartExecutor, 'flutter.ohos.tpns/event_click');
// this.eventChannel.setStreamHandler({
// onListen(args: object, events: EventSink): void {
// this.eventSink = events;
// },
// onCancel(args: object): void {
// this.eventSink = undefined;
// }
// });
}
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
super.onCreate(want, launchParam);
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate' + JSON.stringify(want));
if (want && want.parameters) {
try {
// want判断消息是否是腾讯云推送的消息
// const data = TPushHelper.prototype.parseNotificationMessage(want);
const data = want.parameters;
if (data != null) {
hilog.info(0x0000, 'testTag', '%{public}s', "onCreate" + 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.eventSink?.success(value);
// this.channel?.invokeMethod("getIntent",value)
// 设置方法调用处理器,接收Flutter的指令
this.channel?.setMethodCallHandler({
onMethodCall : (call: MethodCall, result: MethodResult) => {
// console.log(`${TAG} 收到Flutter调用:${call.method},参数:${JSON.stringify(call.args)}`);
// 根据Flutter传的指令名,分情况处理
switch (call.method) {
// 处理"读取数据"指令
case "getIntent":
result.success(value)
break;
// 未知指令,返回"未实现"
default:
result.notImplemented();
break;
}
}
});
} });
} else {
hilog.info(0x0000, 'testTag', 'EntryAbility data was null');
// this.eventSink?.error(0x0000.toString(), 'EntryAbility data was null', null);
}
} catch (err) {
hilog.error(0x0000, 'testTag', 'TPushHelper parseNotificationMessage exception', err);
// this.eventSink?.error(0x0000.toString(), 'TPushHelper parseNotificationMessage exception', err);
}
} else {
hilog.info(0x0000, 'testTag', 'EntryAbility want or want.parameters was null');
// this.eventSink?.error(0x0000.toString(), 'EntryAbility want or want.parameters was null', 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 data = TPushHelper.prototype.parseNotificationMessage(want);
//
// 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.eventSink?.success(value);
// this.channel?.invokeMethod("getIntent",value)
//
// }
//
// });
//
// } else {
// hilog.info(0x0000, 'testTag', 'EntryAbility data was null');
// this.eventSink?.error(0x0000.toString(), 'EntryAbility data was null', null);
// }
// } catch (err) {
// hilog.error(0x0000, 'testTag', 'TPushHelper parseNotificationMessage exception', err);
// this.eventSink?.error(0x0000.toString(), 'TPushHelper parseNotificationMessage exception', err);
// }
// } else {
// hilog.info(0x0000, 'testTag', 'EntryAbility want or want.parameters was null');
// this.eventSink?.error(0x0000.toString(), 'EntryAbility want or want.parameters was null', null);
// }
//
//
// }
}