mave依賴
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.3.7</version>
</dependency>
private static final String APP_KEY ="自己的APP_KEY";
private static final String MASTER_SECRET = "自己的MASTER_SECRET ";
/***
* 通用推送方法
* @param userId 用戶ID
* @param msg 消息
*/
public String messagePush(Integer userId, String clienType, String msg, Map<String,String> map, Integer type){
JPushClient jpushClient = new JPushClient(MASTER_SECRET,APP_KEY , null, ClientConfig.getInstance());
PushPayload payload =null;
try{
if("1".equals(clienType)){
payload = buildPushObject_android_tag_alertWithTitle(userId+"",msg,map,type);
PushResult result = jpushClient.sendPush(payload);
return result.statusCode+"";
}
if("2".equals(clienType)){
payload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage(userId+"",msg,map,type);
PushResult result = jpushClient.sendPush(payload);
return result.statusCode+"";
}
}catch (Exception e){
LOGGER.error(e.getMessage());
}finally {
jpushClient.close();
}
return "客戶端不存在";
}
public PushPayload buildPushObject_android_tag_alertWithTitle(String userId,String msg,Map<String,String> map,Integer type) {
map.put("alert",msg); // 消息
map.put("title","標(biāo)題"); // 標(biāo)題
JSONObject jsonObject = JSONObject.fromObject(map);
PushPayload pushPayload = null;
if(type.intValue()==0){
pushPayload = PushPayload.newBuilder()
.setNotification(Notification.newBuilder().addPlatformNotification(
AndroidNotification.newBuilder()
.setAlert(msg)
.setTitle("間書")
.addExtras(map)
.build()
).build())
.setOptions(Options.newBuilder().setApnsProduction(true).build())
.setPlatform(Platform.android()) // 發(fā)送Android
.setAudience(Audience.alias(userId)) // Audience.alias("userId") Audience.alias(userId+"")
/*.setMessage(Message.content(jsonObject.toString()))*/
.build();
}else{
pushPayload = PushPayload.newBuilder()
.setPlatform(Platform.android()) // 發(fā)送Android
.setAudience(Audience.alias(userId)) // Audience.alias("userId") Audience.alias(userId+"")
.setMessage(Message.content(jsonObject.toString()))
.build();
}
return pushPayload;
}
public PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage(String userId,String msg,Map<String,String> map,Integer type) {
map.put("alert",msg);
JSONObject jsonObject = JSONObject.fromObject(map);
PushPayload pushPayload = null;
if(type.intValue()==0){
pushPayload = PushPayload.newBuilder()
.setPlatform(Platform.ios())
.setAudience(Audience.alias(userId))
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder()
.setContentAvailable(true)
.setAlert(msg)
.setBadge(0) // 角標(biāo)數(shù)
.setSound("happy") // 通知聲音
.addExtras(map)
.build())
.build())
.setOptions(Options.newBuilder()
.setApnsProduction(false) // true-推送生產(chǎn)環(huán)境 false-推送開發(fā)環(huán)境(測試使用參數(shù))
.build())
.build();
}else{
pushPayload = PushPayload.newBuilder()
.setPlatform(Platform.ios())
.setAudience(Audience.alias(userId))
.setMessage(Message.content(jsonObject.toString()))
.setOptions(Options.newBuilder()
.setApnsProduction(false) // true-推送生產(chǎn)環(huán)境 false-推送開發(fā)環(huán)境(測試使用參數(shù))
.build())
.build();
}
return pushPayload;
}
/***
* 更新綁定設(shè)備
* @param registrationId
* @param userId
*/
public void updateDeviceTagAlias(String registrationId,Integer userId){
JPushClient jpushClient = new JPushClient(MASTER_SECRET,APP_KEY , null, ClientConfig.getInstance());
try{
jpushClient.updateDeviceTagAlias(registrationId,userId.toString(),null,null);
}catch (Exception e){
LOGGER.error(e.getMessage());
}finally {
jpushClient.close();
}
}
/***
* 刪除設(shè)備信息
* @param usreId
*/
public void deleteAlias(Integer usreId){
JPushClient jpushClient = new JPushClient(MASTER_SECRET,APP_KEY , null, ClientConfig.getInstance());
try{
jpushClient.deleteAlias(usreId.toString(),null);
}catch (Exception e){
LOGGER.error(e.getMessage());
}finally {
jpushClient.close();
}
}
/***
* 根據(jù)設(shè)備 查信息
* @param registrationId
* @return
*/
public String getDeviceTagAlias(String registrationId){
JPushClient jpushClient = new JPushClient(MASTER_SECRET,APP_KEY , null, ClientConfig.getInstance());
String pushPayload = null;
try {
pushPayload = jpushClient.getDeviceTagAlias("設(shè)備").toString();
}catch (Exception e){
LOGGER.error(e.getMessage());
}finally {
jpushClient.close();
}
return pushPayload;
}