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();
网友评论