flutter_push_method_channel.dart 1.84 KB
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'flutter_push_platform_interface.dart';
import 'mix_push_plugin_listener.dart';

/// An implementation of [FlutterPushPlatform] that uses method channels.
class MethodChannelFlutterPush extends FlutterPushPlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('ewin:flutter_push');
  XiaoMiPushPluginListener? listener;

  @override
  Future<String?> getPlatformVersion() async {
    final version =
        await methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }

  /// 初始化,如果没有appId或appKey,请登录小米开发者中心申请
  /// [appId] 应用appId
  /// [appKey] 应用appKey
  Future<void> init({
    String? defaultPlatform,
    String? appId,
    String? appKey,
  }) async {
    return await methodChannel.invokeMethod('init', {
      "defaultPlatform":defaultPlatform,
      "appId": appId,
      "appKey": appKey,
    });
  }

  Future<bool> checkPermission() async {
    return await methodChannel.invokeMethod('checkPermission');
  }

  Future<bool> getPermission() async {
    return await methodChannel.invokeMethod('getPermission');
  }

  /// 添加消息监听
  void addListener(ListenerValue func) {
    listener ??= XiaoMiPushPluginListener(methodChannel);
    listener!.addListener(func);
  }

  /// 移除消息监听
  void removeListener(ListenerValue func) {
    listener ??= XiaoMiPushPluginListener(methodChannel);
    listener!.removeListener(func);
  }

  /// 获取客户端的 RegId
  Future<String?> getRegId() async {
    return await methodChannel.invokeMethod('getRegId');
  }

  /// 获取客户端的 RegId
  Future<String?> msgReceive() async {
    return await methodChannel.invokeMethod('msgReceive');
  }
}