com.hn.push.jpush.JpushService Maven / Gradle / Ivy
package com.hn.push.jpush;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.hn.push.dto.PushEntity;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
/**
* 极光推送
* @author T Wang.
* 2019/08/15
*/
public class JpushService {
protected String appKey;
protected String masterSecret;
protected Boolean isProduct;
private static final Log log = LogFactory.get();
public JpushService(String appKey,String masterSecret,Boolean isProduct){
this.appKey = appKey;
this.masterSecret = masterSecret;
this.isProduct = isProduct;
}
/**
* 个推发送通知
* @param pushEntity {@link PushEntity}
* @return {string}
*/
public String pushMsgSingle(final PushEntity pushEntity) {
String resultStr = "";
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
PushPayload payload = buildPushObjectSingle(pushEntity);
try {
PushResult result = jpushClient.sendPush(payload);
log.info("个推消息推送结果:" + result);
String iosResultStr = result.toString();
if (iosResultStr.indexOf("msg_id") != -1) {
resultStr = iosResultStr;
}
} catch (Exception e) {
log.error(e);
log.error("个推消息推送通知异常:" + e.fillInStackTrace());
}
return resultStr;
}
/**
* 群推发送通知
* @param pushEntity {@link PushEntity}
* @return {string}
*/
public String pushMsgGroup(final PushEntity pushEntity) {
String resultStr = "";
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
PushPayload payload = buildPushObjectBatched(pushEntity);
try {
PushResult result = jpushClient.sendPush(payload);
log.info("群推消息推送结果:" + result);
String iosResultStr = result.toString();
if (iosResultStr.indexOf("msg_id") != -1) {
resultStr = iosResultStr;
}
} catch (Exception e) {
log.error(e);
log.error("群推消息推送通知异常:" + e.fillInStackTrace());
}
return resultStr;
}
/**
* 构建个推消息发送实体
* @param pushEntity {@link PushEntity}
* @return PushPayload
*/
protected PushPayload buildPushObjectSingle(PushEntity pushEntity) {
try {
return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias(pushEntity.getSingleUserAlias()))
.setNotification(Notification.newBuilder().setAlert(pushEntity.getTitle())
.addPlatformNotification(AndroidNotification.newBuilder()
.addExtra("title", pushEntity.getTitle())
.addExtra("content", pushEntity.getContent())
.addExtra("paramJson", pushEntity.getParamJson().toString())
.build())
.addPlatformNotification(IosNotification.newBuilder().incrBadge(1)
.addExtra("title", pushEntity.getTitle())
.addExtra("content", pushEntity.getContent())
.addExtra("paramJson", pushEntity.getParamJson().toString())
.build())
.build())
.setOptions(Options.newBuilder().setApnsProduction(isProduct).build()).build();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 构建群推消息发送实体
* @param pushEntity {@link PushEntity}
* @return PushPayload
*/
protected PushPayload buildPushObjectBatched(PushEntity pushEntity) {
try {
Collection batchedUserAlias = pushEntity.getSingleUserAlias();
// 去空
batchedUserAlias.removeAll(Collections.singleton(null));
int size = batchedUserAlias.size();
String[] alias = new String[size];
int i = 0;
Iterator iterator = batchedUserAlias.iterator();
while (iterator.hasNext()) {
alias[i] = iterator.next();
i++;
}
return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias(alias))
.setNotification(Notification.newBuilder().setAlert(pushEntity.getTitle())
.addPlatformNotification(AndroidNotification.newBuilder()
.addExtra("title", pushEntity.getTitle())
.addExtra("content", pushEntity.getContent())
.addExtra("paramJson", pushEntity.getParamJson().toString())
.build())
.addPlatformNotification(IosNotification.newBuilder().incrBadge(+1)
.setSound("default")
.addExtra("title", pushEntity.getTitle())
.addExtra("content", pushEntity.getContent())
.addExtra("paramJson", pushEntity.getParamJson().toString())
.build())
.build())
.setOptions(Options.newBuilder().setApnsProduction(isProduct).build()).build();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy