美文网首页
极光推送(极光的插件)

极光推送(极光的插件)

作者: wangyu2488 | 来源:发表于2020-01-19 09:18 被阅读0次

    2020年1月17日

    jpush_flutter: ^0.1.0
    

    https://pub.dev/packages/jpush_flutter

    1.构造如下方法类:

    class JPushUtil {
      JPush jPush;
      // 工厂模式
      factory JPushUtil() => _getInstance();
      static JPushUtil get instance => _getInstance();
      static JPushUtil _instance;
      JPushUtil._internal() {
        // 初始化
        jPush = JPush();
      }
      static JPushUtil _getInstance() {
        if (_instance == null) {
          _instance = new JPushUtil._internal();
        }
        return _instance;
      }
      //目的怕注册失败无法重新setup
      _initJPush(){
        jPush.setBadge(0);
        jPush.setup(
          appKey: Config.JPUSH_APP_KEY,
          channel: "developer-default",
          production: true,
        );
        jPush.applyPushAuthority(NotificationSettingsIOS(sound: true, alert: true, badge: true));
        try {
          jPush.addEventHandler(
              onReceiveNotification: (Map<String,dynamic>message) async {
                print('极光推送 接收到推送:${message}');
              }
          );
        } on Exception {
          print("极光推送 获取平台版本失败");
        }
      }
    
       void sendJPushRegistrationIDToServer() async {
        _initJPush();
        var account = AccountCacheUtil.getAccount();
        if(account != null){
          jPush.getRegistrationID().then((rid) {
            print('极光推送 设备ID: $rid');
            if (AccountCacheUtil.isLogin() && rid.isNotEmpty) {
              var vo = BindJPushRequestVO();
              vo.appId = '11';
              vo.deviceNumber = rid;
              vo.hospitalId = '${account.hospitalId}';
              vo.userId = '${account.id}';
              vo.phoneType = Application.getPlatform().toString();
              HttpUtil.postJson(AccountApi.BIND_JPUSH_DEVICE, parameter: vo,);
            }
          });
        }
      }
    }
    
    

    2.main方法如下使用即可 [登录之后在调用一致将rid创给后端即可]

    JPushUtil().sendJPushRegistrationIDToServer();
    

    相关文章

      网友评论

          本文标题:极光推送(极光的插件)

          本文链接:https://www.haomeiwen.com/subject/bgxrzctx.html