XiaoMiPushPluginListener constructor

XiaoMiPushPluginListener(
  1. MethodChannel channel
)

Implementation

XiaoMiPushPluginListener(MethodChannel channel) {
  // 绑定监听器
  channel.setMethodCallHandler((methodCall) async {
    // 解析参数
    Map<dynamic, dynamic>? arguments = methodCall.arguments;

    switch (methodCall.method) {
      case 'onListener':
        // 获得原始类型和参数
        String typeStr = arguments!['type'].toString();
        var params = arguments['params'] != null
            ? jsonDecode(arguments["params"])
            : null;

        // 封装回调类型和参数
        XiaoMiPushListenerTypeEnum? type;

        // 初始化类型
        for (var item in XiaoMiPushListenerTypeEnum.values) {
          var es = item.toString().split(".");
          if (es[es.length - 1] == typeStr) {
            type = item;
            break;
          }
        }

        // 没有找到类型就返回
        if (type == null) {
          throw MissingPluginException();
        }

        // 回调触发
        switch (type) {
          case XiaoMiPushListenerTypeEnum.RequirePermissions:
            break;
          case XiaoMiPushListenerTypeEnum.NotificationMessageClicked:
            params = MiPushMessageEntity.fromJson(params);
            break;
          case XiaoMiPushListenerTypeEnum.ReceivePassThroughMessage:
            params = MiPushMessageEntity.fromJson(params);
            break;
          case XiaoMiPushListenerTypeEnum.CommandResult:
            params = MiPushCommandMessageEntity.fromJson(params);
            break;
          case XiaoMiPushListenerTypeEnum.ReceiveRegisterResult:
            params = MiPushCommandMessageEntity.fromJson(params);
            break;
          case XiaoMiPushListenerTypeEnum.NotificationMessageArrived:
            params = MiPushMessageEntity.fromJson(params);
            break;
        }

        for (var item in listeners) {
          item(type, params);
        }

        break;
      default:
        throw MissingPluginException();
    }
  });
}