EntryAbility.ets 7.42 KB

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 { TPushHelper } from '../../../../oh_modules/tpns_flutter_plugin/src/main/ets/components/plugin/helper/TPushHelper';
import hilog from '@ohos.hilog';
// import { CacheManager } from '../../../../oh_modules/tpns_flutter_plugin/src/main/ets/components/plugin/helper/CacheManager';

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);
    }


    // hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate' + JSON.stringify(want));
    // if(want && want.parameters ){
    //   try {
    //     const data = TPushHelper.prototype.parseNotificationMessage(want);
    //     if (data) {
    //       hilog.info(0x0000, 'testTag', 'EntryAbility Receive notificationMessage:' + JSON.stringify(data));
    //     } 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');
    // }

    // flutter版本更新后使用 getFlutterEngine().getDartExecutor().getBinaryMessenger()代替 getFlutterView()


    // const  channel = MethodChannel(this.getFlutterEngine()?.getDartExecutor()?.getBinaryMessenger()!!,this.CHANNEL,StandardMethodCodec.INSTANCE);
    // var channel = MethodChannel(getFlutterEngine()?.getDartExecutor()?.getBinaryMessenger()!!, CHANNEL)
    //
    // // 这里要获取启动时的intent
    // val uri = intent.data
    // if(intent.data != null){
    //   Log.i("flutter","onCreate"+intent.data.toString())
    // }
    // val paramsMap = mutableMapOf<String, String>()
    // if (uri != null) {
    //   val set = uri.queryParameterNames
    //   for (name in set) {
    //     val value = uri.getQueryParameter(name)
    //     paramsMap[name.toString()] = value.toString()
    //   }
    // }
    //
    // intentMap = paramsMap
    //

    // this.channel?.setMethodCallHandler((){
    //
    // })

    // channel.setMethodCallHandler { call, result ->
    // Log.i(TAG,intentMap.toString());
    //   when (call.method) {
    //     "getIntent" -> get_intent(call, result)
    //   }
    // }

  }


  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);
    }


  }
}