EntryAbility.ets
4.13 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
import { FlutterAbility, FlutterEngine } from '@ohos/flutter_ohos';
import { GeneratedPluginRegistrant } from '../plugins/GeneratedPluginRegistrant';
import { NativePigeonRegistrar } from '../native/NativePigeonRegistrar';
import { WeChatLoginBridge } from '../native/WeChatLoginBridge';
import { WeChatHostApi } from '../pigeon/WeChatApi';
import Want from '@ohos.app.ability.Want';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import Any from '@ohos/flutter_ohos/src/main/ets/plugin/common/Any';
import MethodCall from '@ohos/flutter_ohos/src/main/ets/plugin/common/MethodCall';
import MethodChannel, { MethodCallHandler, MethodResult } from '@ohos/flutter_ohos/src/main/ets/plugin/common/MethodChannel';
import { BusinessError } from '@kit.BasicServicesKit';
import { pushService } from '@kit.PushKit';
export default class EntryAbility extends FlutterAbility implements MethodCallHandler {
private static readonly FLUTTER_MAIN_CHANNEL = 'doublefeel_flutter_main_channel';
private static readonly OHOS_PUSH_CHANNEL = 'doublefeel_flutter/ohos_push';
private flutterUrlChannel?: MethodChannel;
private pushTokenChannel?: MethodChannel;
private weChatLoginBridge?: WeChatLoginBridge;
configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
GeneratedPluginRegistrant.registerWith(flutterEngine)
NativePigeonRegistrar.register(
flutterEngine.getDartExecutor()!.getBinaryMessenger()!,
this.context,
)
this.flutterUrlChannel = new MethodChannel(
flutterEngine.getDartExecutor()!.getBinaryMessenger()!,
EntryAbility.FLUTTER_MAIN_CHANNEL,
)
this.pushTokenChannel = new MethodChannel(
flutterEngine.getDartExecutor()!.getBinaryMessenger()!,
EntryAbility.OHOS_PUSH_CHANNEL,
)
this.pushTokenChannel.setMethodCallHandler(this)
NativePigeonRegistrar.setAppForeground(true)
NativePigeonRegistrar.handleWant(this.getWant())
this.weChatLoginBridge = new WeChatLoginBridge(this.context)
WeChatHostApi.setup(
flutterEngine.getDartExecutor()!.getBinaryMessenger()!,
this.weChatLoginBridge,
)
this.weChatLoginBridge.handleWant(this.getWant())
}
onMethodCall(call: MethodCall, result: MethodResult): void {
if (call.method !== 'getPushToken') {
result.notImplemented()
return
}
// Push Kit obtains a token asynchronously. Do not request notification
// permission here: token issuance and the user's notification preference
// are independent, and permission is requested by the product flow.
pushService.getToken()
.then((token: string) => result.success(token))
.catch((error: BusinessError) => {
result.error('push_token_unavailable', error.message, error.code)
})
}
onNewWant(want: Want, launchParams: AbilityConstant.LaunchParam): void {
super.onNewWant(want, launchParams)
NativePigeonRegistrar.handleWant(want)
this.forwardFlutterUrl(want)
this.weChatLoginBridge?.handleWant(want)
}
/**
* Mirrors iOS notification/deep-link handling for an existing Flutter
* engine. Keep the URL cached until Flutter confirms it handled the route,
* so a not-yet-ready engine can still consume it during startup.
*/
private forwardFlutterUrl(want: Want): void {
const url = want.uri;
if (url === undefined || url.length === 0) {
return;
}
const result: MethodResult = {
success: (handled: Any): void => {
if (handled === true) {
NativePigeonRegistrar.clearUnhandedUrl(url)
}
},
error: (_code: string, _message: string, _details: Any): void => {},
notImplemented: (): void => {},
}
this.flutterUrlChannel?.invokeMethod('handleFlutterUrl', { url }, result)
}
onForeground(): void {
super.onForeground()
NativePigeonRegistrar.setAppForeground(true)
// A normal return from WeChat has no SendAuthResp callback. Release the
// pending authorization request so Flutter can remove its loading overlay.
this.weChatLoginBridge?.cancelPendingAuthorization()
}
onBackground(): void {
super.onBackground()
NativePigeonRegistrar.setAppForeground(false)
}
}