initPlatformState static method

Future<void> initPlatformState()

Implementation

static Future<void> initPlatformState() async {
  try {
    jpush.addEventHandler(
        onReceiveNotification: (Map<String, dynamic> message) async {
      MyLogUtil.d('flutter onReceiveNotification: $message');
    }, onOpenNotification: (Map<String, dynamic> message) async {
      MyLogUtil.d('flutter onOpenNotification: $message');
      // 暂时暴力清空
      setBadge(0);
      Map<String, dynamic> msg = message;
      String orderNumber = '';
      if (Platform.isAndroid) {
        // 兼容本地推送
        msg.entries.forEach((element) {
          if (element.key == 'extras') {
            String str = element.value['cn.jpush.android.EXTRA'];
            Map<String, dynamic> extraMap = json.decode(str);
            orderNumber = extraMap['orderId'];
          }
        });
      } else {
        orderNumber = msg['orderId'] ?? '';
        if (orderNumber.isBlank == true) {
          orderNumber = msg['extras']['orderId'] ?? '';
        }
      }
      if (orderNumber.isBlank == false) {}
    }, onReceiveMessage: (Map<String, dynamic> message) async {
      MyLogUtil.d('flutter onReceiveMessage: $message');
    }, onReceiveNotificationAuthorization:
            (Map<String, dynamic> message) async {
      MyLogUtil.d('flutter onReceiveNotificationAuthorization: $message');
    }, onNotifyMessageUnShow: (Map<String, dynamic> message) async {
      MyLogUtil.d('flutter onNotifyMessageUnShow: $message');
    });
  } on PlatformException {
    MyLogUtil.e('Failed to get platform version.');
  }

  jpush.setAuth(enable: true);
  jpush.setup(
    appKey: appPushKey,
    channel: '',
    production: false,
    debug: true,
  );
  jpush.applyPushAuthority(
      const NotificationSettingsIOS(sound: true, alert: true, badge: true));

  jpush.getRegistrationID().then((rid) {
    MyLogUtil.d('flutter get registration id : $rid');
    StorageService storageService = StorageService.instance;
    if (rid.isBlank == false) {
      if (storageService.getString(StorageValue.userDeviceToken) == null ||
          storageService.getString(StorageValue.userDeviceToken).isBlank ==
              true) {
        storageService.setString(StorageValue.userDeviceToken, rid);
      }
    }
  });
}