顾海波

【需求】鸿蒙接入获取token

/node_modules
/oh_modules
/local.properties
/.idea
**/build
/.hvigor
.cxx
/.clangd
/.clang-format
/.clang-tidy
**/.test
/entry/libs/arm64-v8a/libflutter.so
/entry/src/main/resources/rawfile/flutter_assets
**.har
**/oh-package-lock.json5
BuildProfile.ets
... ...
/*
* Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
{
"apiType": "stageMode",
"buildOption": {
},
"targets": [
{
"name": "default"
},
{
"name": "flutter_push",
}
]
}
... ...
/*
* Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { harTasks } from '@ohos/hvigor-ohos-plugin';
... ...
/*
* Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import PushPlugin from './src/main/ets/com/ewin/flutter_push/FlutterPushPlugin';
export default PushPlugin;
... ...
/*
* Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
{
"name": "flutter_push",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "index.ets",
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@ohos/flutter_ohos": "file:./libs/flutter_embedding.har"
}
}
... ...
/*
* Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
FlutterPlugin,
FlutterPluginBinding
} from '@ohos/flutter_ohos/src/main/ets/embedding/engine/plugins/FlutterPlugin';
import MethodChannel from '@ohos/flutter_ohos/src/main/ets/plugin/common/MethodChannel';
import PushPluginMethodChannelHandler from './PushPluginMethodChannelHandler';
import Log from '@ohos/flutter_ohos/src/main/ets/util/Log';
import StandardMethodCodec from '@ohos/flutter_ohos/src/main/ets/plugin/common/StandardMethodCodec';
const TAG: string = "PushPlugin";
export default class PushPlugin implements FlutterPlugin {
private methodChannel: MethodChannel | null = null;
private methodChannelHandler: PushPluginMethodChannelHandler | null = null;
private pluginBing: FlutterPluginBinding | null = null;
getUniqueClassName(): string {
return "PushPlugin";
}
onAttachedToEngine(binding: FlutterPluginBinding): void {
Log.d(TAG, 'onAttachedToEngine connectivity plugin')
this.methodChannel = new MethodChannel(binding.getBinaryMessenger(), "ewin:flutter_push", StandardMethodCodec.INSTANCE);
let methodChannelHandler = new PushPluginMethodChannelHandler(this.methodChannel);
this.methodChannel.setMethodCallHandler(methodChannelHandler);
this.pluginBing = binding;
}
onDetachedFromEngine(binding: FlutterPluginBinding): void {
Log.d(TAG, 'onDetachedFromEngine connectivity plugin')
this.methodChannel?.setMethodCallHandler(null);
this.methodChannel = null;
this.pluginBing = null;
}
}
... ...
/*
* Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import MethodCall from '@ohos/flutter_ohos/src/main/ets/plugin/common/MethodCall';
import { MethodCallHandler, MethodResult } from '@ohos/flutter_ohos/src/main/ets/plugin/common/MethodChannel';
import Log from '@ohos/flutter_ohos/src/main/ets/util/Log';
import { pushService } from '@kit.PushKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit';
import { MethodChannel } from '@ohos/flutter_ohos';
const TAG: string = "PushPluginMethodChannelHandler";
export default class PushPluginMethodChannelHandler implements MethodCallHandler {
private methodChannel: MethodChannel | null = null;
constructor(methodChannel:MethodChannel) {
this.methodChannel = methodChannel
}
onMethodCall(call: MethodCall, result: MethodResult): void {
Log.d(TAG, 'onMethodCall step in')
switch (call.method) {
case "init":
this.init(result);
break;
case "getRegId":
break;
default:
result.notImplemented();
break;
}
}
async init(result: MethodResult): Promise<void> {
try {
const pushToken: string = await pushService.getToken();
const map:Map<string,string> =new Map<string,string>()
const params:Map<string,string> =new Map<string,string>()
params.set("platformName","huawei_ohos")
params.set("regId",pushToken)
map.set("type","ReceiveRegisterResult")
map.set("params", JSON.stringify(params))
this.methodChannel?.invokeMethod("onListener",map)
} catch (err) {
let e: BusinessError = err as BusinessError;
hilog.error(0x0000, 'testTag', 'Failed to get push token: %{public}d %{public}s', e.code, e.message);
}
result.success(true)
}
}
... ...
/*
* Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
{
"module": {
"name": "flutter_push",
"type": "har",
"deviceTypes": [
"default",
],
"requestPermissions": [
{
"name": "ohos.permission.INTERNET"
},
{
"name": "ohos.permission.GET_NETWORK_INFO"
}
]
}
}
... ...
{
"string": [
{
"name": "page_show",
"value": "page from npm package"
}
]
}
... ...
{
"string": [
{
"name": "page_show",
"value": "page from npm package"
}
]
}
... ...
{
"string": [
{
"name": "page_show",
"value": "page from npm package"
}
]
}
... ...
... ... @@ -51,6 +51,9 @@ flutter:
web:
pluginClass: FlutterPushWeb
fileName: flutter_push_web.dart
ohos:
package: com.ewin.flutter_push
pluginClass: FlutterPushPlugin
# To add assets to your plugin package, add an assets section, like this:
# assets:
... ...