https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/
不哔哔,上图,通俗易懂!
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import cn.jiguang.common.ClientConfig;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
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.Notification;
import cn.jpush.api.push.model.notification.AndroidNotification;
@Service
public class JpushServiceImpl implements JpushService {
private static final Logger LOG = Logger.getLogger(JpushServiceImpl.class);
@Resource
public JpushKey jpushKey;
@Resource
private MessagePushMapper mpmapper;
@Override
public String addPush(MessagePush mPush) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = formatter.parse("2018-06-08 10:00:00");
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 设置发送开始时间
mPush.setCreatetime(date);
// 设置离线时间
ClientConfig clientConfig = ClientConfig.getInstance();
clientConfig.setTimeToLive(jpushKey.getTimeover());// 秒
// 肯定是安卓端 ,那就发送到全部
JPushClient jpushClient = new JPushClient(jpushKey.getMasterSecret(), jpushKey.getAppKey(), null, clientConfig);
PushPayload payload = null;
// 这里判断tag,也就是发送给哪些人
if (mPush.getTag() == null || "".equals(mPush.getTag())) {
// payload = PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.all()).setNotification(Notification.android(mPush.getContent(), mPush.getTitle(), null)).build();
payload = PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.all()).setNotification(Notification.newBuilder().addPlatformNotification(AndroidNotification.newBuilder().setAlert(mPush.getContent()).setTitle(mPush.getTitle()).setBuilderId(3).setPriority(1).setStyle(2).build()).build()).build();
} else {
String[] tags = mPush.getTag().split(",");
// payload = PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.tag(tags)).setNotification(Notification.android(mPush.getContent(), mPush.getTitle(), null)).build();
payload = PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.tag(tags)).setNotification(Notification.newBuilder().addPlatformNotification(AndroidNotification.newBuilder().setAlert(mPush.getContent()).setTitle(mPush.getTitle()).setBuilderId(3).setPriority(1).setStyle(2).build()).build()).build();
}
try {
PushResult result = jpushClient.sendPush(payload);
} catch (Exception e) {
mPush.setSendtime(new Date());
mPush.setSendstatus("失败");
// mpmapper.insert(mPush);
LOG.debug(e.getStackTrace());
return "消息接收人目前未重新登陆新版app!";
}
// 发送内容落地
mPush.setSendstatus("成功");
mPush.setSendtime(new Date());
// mpmapper.insert(mPush);
return "推送成功!";
}
public JpushKey getJpushKey() {
return jpushKey;
}
public void setJpushKey(JpushKey jpushKey) {
this.jpushKey = jpushKey;
}
public Date addOneDay(Date sendEnd) {
Calendar cd = Calendar.getInstance();
cd.setTime(sendEnd);
cd.add(Calendar.DATE, 1);//增加一天
return cd.getTime();
}
}