xg_android_api.dart 17.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
import 'dart:async';

import 'package:flutter/services.dart';

import 'native_event_handler.dart';

class XgAndroidApi {
  final String flutterLog = "| XGPUSH | Flutter | ";
  static MethodChannel? _channel;
  NativeEventHandler? _eventHandler;

  XgAndroidApi(MethodChannel channel) {
    _channel = channel;
  }

  ///在调native函数前需要调用改函数
  void addNativeEventHandler(NativeEventHandler eventHandler) {
    this._eventHandler = eventHandler;
    _channel?.setMethodCallHandler(_handleMethod);
  }

  Future<Null> _handleMethod(MethodCall call) async {
    if (this._eventHandler == null) return;
    Map para = call.arguments;
    switch (call.method) {
      case "onRegisterPushSuccess":
        String token = para['pushToken'];
        this._eventHandler?.onRegisterPushSuccess(token);
        return;
      case "onRegisterPushFail":
        String message = para['message'];
        int code = para['resultCode'];
        this._eventHandler?.onRegisterPushFail(message, code);
        return;
      case "onUnregisterResult":
        String message = para['message'];
        int code = para['resultCode'];
        this._eventHandler?.onUnregisterResult(message, code);
        return;
      case "onTextMessage":
        String message = para['message'];
        String customContent = para['customMessage'];
        String title = para['title'];
        this._eventHandler?.onTextMessage(title, message, customContent);
        return;
      case "onSetTagResult":
        String message = para['message'];
        int code = para['resultCode'];
        this._eventHandler?.onSetTagResult(message, code);
        return;
      case "onDeleteTagResult":
        String message = para['message'];
        int code = para['resultCode'];
        this._eventHandler?.onDeleteTagResult(message, code);
        return;
      case "onNotifactionShowedResult":
        String message = para['message'];
        String customContent = para['customMessage'];
        String title = para['title'];
        int actionType = para['actionType'];
        this._eventHandler?.onNotifactionShowedResult(
            title, message, customContent, actionType);
        return;
      case "onNotifactionClickedResult":
        String message = para['message'];
        String customContent = para['customMessage'];
        String title = para['title'];
        int actionType = para['actionType'];
        this._eventHandler?.onNotifactionClickedResult(
            title, message, customContent, actionType);
        return;
      case "onBindAccountResult":
        String message = para['message'];
        String account = para['account'];
        int code = para['resultCode'];
        this._eventHandler?.onBindAccountResult(account, code, message);
        return;
      case "onAppendAccountResult":
        String message = para['message'];
        String account = para['account'];
        int code = para['resultCode'];
        this._eventHandler?.onAppendAccountResult(account, code, message);
        return;
      case "onDeleteAccountResult":
        String message = para['message'];
        String account = para['account'];
        int code = para['resultCode'];
        this._eventHandler?.onDeleteAccountResult(account, code, message);
        return;
    }
  }

  ///注册信鸽推送
  ///production
  ///debug 是否为debug模式  默认不是
  void regPush() {
    _channel?.invokeMethod('regPush');
  }

   /// 设置接入点域名
  void setServerSuffix({String? serverAddr}) {
    _channel?.invokeMethod('setServerSuffix', {'addr': serverAddr});
  }

   /// 设置accessid
  void setAccessId({String? accessId}) {
    _channel?.invokeMethod('setAccessId', {'accessId': accessId});
  }

 /// 设置accessKey
  void setAccessKey({String? accessKey}) {
    _channel?.invokeMethod('setAccessKey', {'accessKey': accessKey});
  }


  ///开启debug模式
  ///debug 是否为debug模式 默认不是
  void setEnableDebug({bool? debug}) {
    _channel?.invokeMethod('setEnableDebug', {'enableDebug': debug});
  }

  /// 设置心跳时间间隔
  /// interval 心跳间隔
  void setHeartbeatIntervalMs({int? interval}) {
    _channel?.invokeMethod(
        'setHeartbeatIntervalMs', {'heartBeatIntervalMs': interval});
  }

  /// 配置是否关闭拉起其他 App
  /// enable 是否关闭 默认不是
  void enablePullUpOtherApp({bool? enable}) {
    _channel?.invokeMethod('enablePullUpOtherApp', {'enable': enable});
  }

  ///反注册信鸽推送
  /// 当用户已退出或 App 被关闭,不再需要接收推送时,可以取消注册 App,即反注册。
  ///(一旦设备反注册,直到这个设备重新注册成功期间内,下发的消息该设备都无法收到)
  void stopXg() {
    _channel?.invokeMethod('stopXg');
  }

  ///设置标签
  void setXgTag({String? tagName}) {
    _channel?.invokeMethod('setXgTag', {'tagName': tagName});
  }

  /// 设置多tag tagNames为List<String>tag的集合
  /// 一次设置多个标签,会覆盖这个设备之前设置的标签。
  void setXgTags({List? tagNames}) {
    _channel?.invokeMethod('setXgTags', {'tagNames': tagNames});
  }

  /// 添加多个标签 tagNames为List<String>tag的集合 每个 tag 不能超过40字节(超过会抛弃)不能包含空格(含有空格会删除空格)
  /// 最多设置1000个 tag,超过部分会抛弃
  /// 一次设置多个标签,会覆盖这个设备之前设置的标签。
  /// <p>
  /// 如果新增的标签的格式为 "test:2 level:2",则会删除这个设备的全部历史标签,再新增 test:2 和 level。
  /// 如果新增的标签有部分不带:号,如 "test:2 level",则会删除这个设备的全部历史标签,再新增 test:2 和 level 标签。
  /// <p>
  /// 新增的 tags 中,:号为后台关键字,请根据具体的业务场景使用。
  /// 此接口调用的时候需要间隔一段时间(建议大于5s),否则可能造成更新失败。
  void addXgTags({List? tagNames}) {
    _channel?.invokeMethod('addXgTags', {'tagNames': tagNames});
  }

  ///删除指定标签
  void deleteXgTag({String? tagName}) {
    _channel?.invokeMethod('deleteXgTag', {'tagName': tagName});
  }

  ///删除多个标签
  ///Set 标签名集合,每个标签是一个 String。限制:每个 tag 不能超过40字节(超过会抛弃),
  ///不能包含空格(含有空格会删除空格)。最多设置1000个tag,超过部分会抛弃。
  void deleteXgTags({List? tagNames}) {
    _channel?.invokeMethod('deleteXgTags', {'tagNames': tagNames});
  }

  ///清除所有标签
  void cleanXgTags() {
    _channel?.invokeMethod('cleanXgTags');
  }

  ///获取token
  /// 第一次注册会产生 Token,之后一直存在手机上,不管以后注销注册操作,该 Token 一直存在,
  /// 当 App 完全卸载重装了 Token 会发生变化。不同 App 之间的 Token 不一样。
  Future<String> getXgToken() async {
    final String token = await _channel?.invokeMethod('xgToken');
    return token;
  }

  ///绑定账号
  ///推荐有账号体系的App使用(此接口会覆盖设备之前绑定过的账号,仅当前注册的账号生效)
  void bindAccount({String? account}) {
    _channel?.invokeMethod('bindAccount', {'account': account});
  }

  void bindAccountWithType({String? account, String? accountType}) {
    _channel?.invokeMethod('bindAccount', {'account': account, 'accountType': accountType});
  }

  ///添加账号
  ///推荐有账号体系的App使用(此接口保留之前的账号,只做增加操作,
  ///一个token下最多只能有10个账号超过限制会自动顶掉之前绑定的账号,)
  void appendAccount({String? account}) {
    _channel?.invokeMethod('appendAccount', {'account': account});
  }

  void appendAccountWithType({String? account, String? accountType}) {
    _channel?.invokeMethod('appendAccount', {'account': account, 'accountType': accountType});
  }

  ///删除注册账号
  ///账号解绑只是解除 Token 与 App 账号的关联,若使用全量/标签/Token 推送仍然能收到通知/消息。
  void delAccount({String? account}) {
    _channel?.invokeMethod('delAccount', {'account': account});
  }
  
  void delAccountWithType({String? account, String? accountType}) {
    _channel?.invokeMethod('delAccount', {'account': account, 'accountType': accountType});
  }

  ///清除全部账号
  void delAllAccount() {
    _channel?.invokeMethod('delAllAccount');
  }

  ///新增用户属性
  void upsertAttributes({Map<String, String>? attributes}) {
    _channel?.invokeMethod('upsertAttributes', {'attributes': attributes});
  }

  ///删除用户属性
  void delAttributes({List<String>? attributes}) {
    _channel?.invokeMethod('delAttributes', {'attributes': attributes});
  }

  ///更新用户属性
  void clearAndAppendAttributes({Map<String, String>? attributes}) {
    _channel?.invokeMethod('clearAndAppendAttributes', {'attributes': attributes});
  }

  ///清除全部用户属性
  void clearAttributes() {
    _channel?.invokeMethod('clearAttributes');
  }

/*-------------第三方厂商通道集成-------------*/

  /// 开启其他推送
  void enableOtherPush() {
    _channel?.invokeMethod('enableOtherPush');
  }

  /// 开启其他推送
  void enableOtherPush2({bool? enable}) {
    _channel?.invokeMethod('enableOtherPush2', {'enable': enable});
  }

  /// 获取厂商推送 token
  Future<String> getOtherPushToken() async {
    final String otherPushToken = await _channel?.invokeMethod('getOtherPushToken');
    return otherPushToken;
  }

  /// 获取厂商推送品牌
  Future<String> getOtherPushType() async {
    final String otherPushType = await _channel?.invokeMethod('getOtherPushType');
    return otherPushType;
  }

  /*-------------------角标控制---------------*/

  /// 接设置应用角标,
  /// 当前支持华为、OPPO、vivo,其中 OPPO 需另外向厂商申请角标展示权限
  void setBadgeNum({int? badgeNum}) {
    _channel?.invokeMethod('setBadgeNum', {'badgeNum': badgeNum});
  }

  /// 设置手机应用角标归0,建议在应用打开时将角标清0,
  /// 当前支持华为、OPPO、vivo,其中 OPPO 需另外向厂商申请角标展示权限
  void resetBadgeNum() {
    _channel?.invokeMethod('resetBadgeNum');
  }


  /// 清空当前应用在通知栏的通知
  void cancelAllNotification() {
    _channel?.invokeMethod('cancelAllNotification');
  }

  /// 创建通知渠道
  void createNotificationChannel({String? channelId, String? channelName}) {
    _channel?.invokeMethod('createNotificationChannel', {'channelId': channelId, 'channelName': channelName});
  }

  /// 创建通知渠道,指定通知提示各项属性
  /// 
  /// soundFileName 直接填写铃声文件名,不带后缀;铃声文件放置在安卓应用模块的资源目录 /res/raw 下
  void createNotificationChannelWithSound({String? channelId, String? channelName, bool? enableVibration, bool? enableLights, bool? enableSound, String? soundFileName}) {
    _channel?.invokeMethod('createNotificationChannel', {'channelId': channelId, 'channelName': channelName, 'enableVibration': enableVibration, 'enableLights': enableLights, 'enableSound': enableSound, 'soundFileName': soundFileName});
  }

  /*----------------小米厂商通道集成-----------*/

  /// 设置小米推送的APP_ID
  /// aappId为在小米平台注册所得Id
  void setMiPushAppId({String? appId}) {
    _channel?.invokeMethod('setMiPushAppId', {'appId': appId});
  }

  /// 设置小米推送的APP_KEY
  ///appKey 为在小米平台注册所得key
  void setMiPushAppKey({String? appKey}) {
    _channel?.invokeMethod('setMiPushAppKey', {'appKey': appKey});
  }

  ///判断是否为小米手机
  Future<bool> isMiuiRom() async {
    final bool isMIUI = await _channel?.invokeMethod('isMiuiRom');
    return isMIUI;
  }

  ///开启小米厂商通道
  ///appId  为在小米平台注册所得Id
  ///appKey 为在小米平台注册所得key
  @Deprecated(
      'Please call the following apis instead:'
        'XgFlutterPlugin.xgApi.setMiPushAppId(appId:"APP_ID");'
        'XgFlutterPlugin.xgApi.setMiPushAppKey(appKey: "APPKEY");'
        'XgFlutterPlugin.xgApi.enableOtherPush();'
        'XgFlutterPlugin.xgApi.regPush();'
  )
  void startMiPush({String? appId, String? appKey}) async {
    final bool isMIUI = await _channel?.invokeMethod('isMiuiRom');
    if (isMIUI) {
      _channel?.invokeMethod('setMiPushAppId', {'appId': appId});
      _channel?.invokeMethod('setMiPushAppKey', {'appKey': appKey});
      _channel?.invokeMethod('enableOtherPush');
      _channel?.invokeMethod('regPush');
    }
  }

  /*---------------------华为厂商通道集成----------------------*/

  ///判断是否为华为手机
  Future<bool> isEmuiRom() async {
    final bool isHUAWEI = await _channel?.invokeMethod('isEmuiRom');
    return isHUAWEI;
  }

  ///开启华为厂商通道
  @Deprecated(
      'Please call the following apis instead:'
          'XgFlutterPlugin.xgApi.enableOtherPush();'
          'XgFlutterPlugin.xgApi.regPush();'
  )
  void startHuaWeiPush() async {
    final bool isHUAWEI = await _channel?.invokeMethod('isEmuiRom');
    if (isHUAWEI) {
      _channel?.invokeMethod('enableOtherPush');
      _channel?.invokeMethod('regPush');
    }
  }

  /*---------------------oppo厂商通道集成------------------*/

  ///开启Oppo通知
  ///isNotification 是否开启OPPO通知
  void enableOppoNotification({bool? isNotification}) {
    _channel?.invokeMethod(
        'enableOppoNotification', {'isNotification': isNotification});
  }

  ///设置Oppo推送ID
  ///appId为在Oppo平台注册所得Id
  void setOppoPushAppId({String? appId}) {
    _channel?.invokeMethod('setOppoPushAppId', {'appId': appId});
  }

  ///设置Oppo推送Key
  ///appKey为在Oppo平台注册所得Key
  void setOppoPushAppKey({String? appKey}) {
    _channel?.invokeMethod('setOppoPushAppKey', {'appKey': appKey});
  }

  ///判断是否为OPPO手机
  Future<bool> isOppoRom() async {
    final bool isOppo = await _channel?.invokeMethod('isOppoRom');
    return isOppo;
  }

  ///开启oppo厂商通道
  ///isNotification 是否开启OPPO通知
  /// appId为在Oppo平台注册所得Id
  /// appKey为在Oppo平台注册所得Key
  @Deprecated(
      'Please call the following apis instead:'
          'XgFlutterPlugin.xgApi.setOppoPushAppId(appId:"APP_ID");'
          'XgFlutterPlugin.xgApi.setOppoPushAppKey(appKey: "APPKEY");'
          'XgFlutterPlugin.xgApi.enableOtherPush();'
          'XgFlutterPlugin.xgApi.regPush();'
  )
  void startOPPOPush({String? appId, String? appKey, bool? isNotification}) async {
    final bool isOppo = await _channel?.invokeMethod('isOppoRom');
    if (isOppo) {
      _channel?.invokeMethod(
          'enableOppoNotification', {'isNotification': isNotification});
      _channel?.invokeMethod('setOppoPushAppId', {'appId': appId});
      _channel?.invokeMethod('setOppoPushAppKey', {'appKey': appKey});
      _channel?.invokeMethod('enableOtherPush');
      _channel?.invokeMethod('regPush');
    }
  }

/*--------------------vivo厂商通道集成---------------*/

  ///判断是否为Vico手机
  Future<bool> isVivoRom() async {
    final bool isVivo = await _channel?.invokeMethod('isVivoRom');
    return isVivo;
  }

  ///开启ViVO厂商通道
  @Deprecated(
      'Please call the following apis instead:'
          'XgFlutterPlugin.xgApi.enableOtherPush();'
          'XgFlutterPlugin.xgApi.regPush();'
  )
  void startViVOPush() async {
    final bool isVivo = await _channel?.invokeMethod('isVivoRom');
    if (isVivo) {
      _channel?.invokeMethod('enableOtherPush');
      _channel?.invokeMethod('regPush');
    }
  }

  /*-------------------魅族厂商通道集成--------------*/

  ///设置魅族推送所需appID
  ///appId为在魅族平台注册所得Id
  void setMzPushAppId({String? appId}) {
    _channel?.invokeMethod('setMzPushAppId', {'appId': appId});
  }

  ///设置魅族推送所需appKey
  ///appKey为在魅族平台注册所得key
  void setMzPushAppKey({String? appKey}) {
    _channel?.invokeMethod('setMzPushAppKey', {'appKey': appKey});
  }

  ///判断是否为魅族手机
  Future<bool> isMeizuRom() async {
    final bool isMeiZu = await _channel?.invokeMethod('isMeizuRom');
    return isMeiZu;
  }

  ///开启魅族手机厂商通道
  ///appId为在魅族平台注册所得Id
  ///appKey为在魅族平台注册所得key
  @Deprecated(
      'Please call the following apis instead:'
          'XgFlutterPlugin.xgApi.setMzPushAppId(appId:"APP_ID");'
          'XgFlutterPlugin.xgApi.setMzPushAppKey(appKey: "APPKEY");'
          'XgFlutterPlugin.xgApi.enableOtherPush();'
          'XgFlutterPlugin.xgApi.regPush();'
  )
  void startMeizuPush({String? appId, String? appKey}) async {
    final bool isMeiZu = await _channel?.invokeMethod('isMeizuRom');
    if (isMeiZu) {
      _channel?.invokeMethod('setMzPushAppId', {'appId': appId});
      _channel?.invokeMethod('setMzPushAppKey', {'appKey': appKey});
      _channel?.invokeMethod('enableOtherPush');
      _channel?.invokeMethod('regPush');
    }
  }

  /*------------------谷歌厂商通道集成---------------*/

  ///判断是否为支持FCM手机
  @Deprecated(
      'TPNS SDK would auto-check if the device supports Firebase-messaging.'
  )
  Future<bool> isFcmRom() async {
    final bool isFcm = await _channel?.invokeMethod('isFcmRom');
    return isFcm;
  }

  ///判断是否为谷歌手机
  Future<bool> isGoogleRom() async {
    final bool isGoogle = await _channel?.invokeMethod('isGoogleRom');
    return isGoogle;
  }

  ///开启谷歌手机厂商通道
  @Deprecated(
      'Please call the following apis instead:'
          'XgFlutterPlugin.xgApi.enableOtherPush();'
          'XgFlutterPlugin.xgApi.regPush();'
  )
  void startFcmPush() async {
    final bool isGoogle = await _channel?.invokeMethod('isGoogleRom');
    if (isGoogle) {
      _channel?.invokeMethod('enableOtherPush');
      _channel?.invokeMethod('regPush');
    }
  }

  ///判断是否为360手机
  Future<bool> is360Rom() async {
    final bool is360 = await _channel?.invokeMethod('is360Rom');
    return is360;
  }
}