XgFlutterPlugin.ets 3.44 KB
import {
  FlutterPlugin,
  FlutterPluginBinding,
  MethodCall,
  MethodCallHandler,
  MethodChannel,
  MethodResult,
} from '@ohos/flutter_ohos';

import TPushHelper from './helper/TPushHelper';
import { UIAbility, Want } from '@kit.AbilityKit';
import { AbilityAware, AbilityPluginBinding } from '@ohos/flutter_ohos';
import Log from '@ohos/flutter_ohos/src/main/ets/util/Log';
import { CacheManager } from './helper/CacheManager';
import { TPush } from 'tpns';


const TAG: string = "TPushOhos";



/** XgFlutterPlugin **/
export default class XgFlutterPlugin implements FlutterPlugin, MethodCallHandler {
  private channel: MethodChannel | null = null;
  private applicationContext: Context | null = null;
  private ability: UIAbility | null = null;



  constructor() {
  }



  getUniqueClassName(): string {
    return "XgFlutterPlugin"
  }

  onAttachedToAbility(binding: AbilityPluginBinding): void {
    Log.i(TAG, "onAttachedToAbility");
    this.ability = binding.getAbility()
  }

  onDetachedFromAbility(): void {
    Log.i(TAG, "onDetachedFromAbility");
    this.ability = 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)
    }
  }

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



 
}