maven 依赖包
cn.jpush.api
jpush-client
3.4.9
极光 接口 url常量 类
public class JiGuangConstant {
public final static String createRoom = "/v1/chatroom/";
public final static String roomBatch = "/v1/chatroom/batch";
public final static String createUser = "/v1/users/";
public final static String push = "/v3/push";
public static String getStateUrl(String user){
return "/v1/users/"+user+"/userstat";
}
极光 service接口
public interface JiGuangService {
public JSONObject createRoom(String paramString) throws Exception;
public JSONArray registUser(String requestString) throws Exception;
void pushAndroidMessage(String title, String method, List androidIds, Message message, int courseId) throws Exception;
JSONObject getUserState(String userName) throws Exception;
极光service 实现类
@Service
public class JiGuangServiceImpl implements JiGuangService {
// TODO 极光IM 实现类接口
@Autowired
private SysConfigService sysConfigService;
@Autowired
private SysLogService syslogService;
@Override
public JSONObject createRoom(String paramString) throws Exception {
String authBasicCode = ServiceHelper.getBasicAuthorization("极光 APPKEY",
"极光 MasterSecret");
return JSONObject.parseObject(JiGuangUtil.requestStringBody(
"https://api.im.jpush.cn" + JiGuangConstant.createRoom,
authBasicCode,
paramString, true));
}
@Override
public JSONArray registUser(String requestString) throws Exception {
String authBasicCode = ServiceHelper.getBasicAuthorization("极光 APPKEY",
"极光 MasterSecret");
String json = JiGuangUtil.requestStringBody(
"https://api.im.jpush.cn"+ JiGuangConstant.createUser,
authBasicCode,
requestString, false);
return JSONObject.parseArray(json);
}
@Override
public JSONObject getUserState(String userName) throws Exception {
String url = "https://api.im.jpush.cn" + JiGuangConstant.getStateUrl(userName) ;
String authBasicCode = ServiceHelper.getBasicAuthorization("极光 APPKEY",
"极光 MasterSecret");
return JSONObject.parseObject(JiGuangUtil.requestGet(url ,
authBasicCode));
}
@Override
public void pushAndroidMessage(String title, String method, List androidIds, Message message,int courseId) throws Exception {
//执行开始时间
long startTime = DateTimeUtil.NOW.getTime();
SysLog sysLog = new SysLog();
//推送参数
PushParam pushParam = new PushParam();
pushParam.setPlatform("android");
if(androidIds!=null && androidIds.size()>0) {
JSONObject audience = new JSONObject();
JSONArray jsonArray = JSONObject.parseArray(JSON.toJSONString(androidIds));
audience.put("registration_id", jsonArray);
pushParam.setAudience(audience);
}
else {
pushParam.setAudience("all");
}
Notification notification = new Notification();
notification.setalert(title);
//设置安卓推送消息
NotificationAndroid android = new NotificationAndroid();
//设置安卓附加字段
//推送课程对应ID
if(courseId > 0) {
JSONObject extra = new JSONObject();
extra.put("type", 0);
extra.put("id", courseId);
android.setExtras(extra);
}
//设置安卓角标与入口类
if(StringUtils.isNotBlank(method)) {
android.setBadge_add_num(1);
android.setBadge_class(method);
}
notification.setAndroid(android);
pushParam.setNotification(notification);
if(message != null) {
pushParam.setMessage(message);
}
//设置日志内容
String authBasicCode = ServiceHelper.getBasicAuthorization("极光 APPKEY",
"极光 MasterSecret");
sysLog.setMsg("安卓推送结果:"+JSONObject.parseObject(JiGuangUtil.requestStringBody(
"https://api.jpush.cn"+ JiGuangConstant.push,
authBasicCode,
JSONObject.toJSONString(pushParam), true)).toJSONString());
sysLog.setTime(DateTimeUtil.caluRunTime(startTime));
sysLog.setCreateDate(DateTimeUtil.NOW);
syslogService.runLog(sysLog);
}
注册极光用户 示例
public void regJGIMUser() throws Exception {
member.setJgId("手机号码" + genCaptchaUtil.randomChar(4));
member.setJgPwd("密码");
member.setNiName("昵称前缀" + "手机号码".substring(7));
JSONObject jgUserCreateParam = new JSONObject();
jgUserCreateParam.put("username", member.getJgId());
jgUserCreateParam.put("password", member.getJgPwd());
jgUserCreateParam.put("nickname", member.getNiName());
List