1.安装
maven方式
将下边的依赖条件放到你项目的 maven pom.xml 文件里
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.3.8</version>
</dependency>
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jiguang-common</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.6.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<!-- For log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2.注册:极光账号
3.utils
package com.ifly.tools.push;
import cn.jiguang.common.ClientConfig;
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.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.beite.tools.utils.PropertiesUtil;
/**
* Created by cxx on 2018/7/9.
*/
public class MsgPush {
private static final Logger LOG = LoggerFactory.getLogger(MsgPush.class);
private static JPushClient jpushClient;
private static boolean flag = false;
private String appKey = PropertiesUtil.getProperty("property_jpush.appkey");
private String masterSecret =PropertiesUtil.getProperty("property_jpush.master.secret");
private String jpushEnvironment=PropertiesUtil.getProperty("jpush.environment");
/**
* 初始化极光服务环境
*/
private MsgPush(){
if (LOG.isInfoEnabled()) {
LOG.info("============jiguang init start...=========");
}
if (LOG.isInfoEnabled()) {
LOG.info("============jiguang -- appKey========="+appKey);
LOG.info("============jiguang -- masterSecret========="+masterSecret);
}
ClientConfig clientConfig = ClientConfig.getInstance();
jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);
if (LOG.isInfoEnabled()) {
LOG.info("============jiguang environment========="+jpushEnvironment);
}
if (jpushEnvironment.equals("true")){
flag = true;
}
if (LOG.isInfoEnabled()) {
LOG.info("============jiguang init end...=========");
}
}
/**
* 单例对象,在系统启动时创建
*/
private static MsgPush instance = new MsgPush();
/**
* 单例构造函数(饿汉)
* @return 单例对象
*/
public static MsgPush getInstance(){
return instance;
}
//通知给所有用户
public static void pushAll(String alert) throws Exception{
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.all())
.setNotification(Notification.alert(alert))
.setOptions(Options.newBuilder()
.setApnsProduction(false)
.setTimeToLive(86400*10)
.build())
.build();
PushResult result = jpushClient.sendPush(payload);
LOG.info("Got result - " + result);
}
//通知给某个用户
public static void push(String alias,String alert) throws Exception{
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.alias(alias))
.setNotification(Notification.alert(alert))
.setOptions(Options.newBuilder()
.setApnsProduction(false)
.setTimeToLive(86400*10)
.build())
.build();
PushResult result = jpushClient.sendPush(payload);
LOG.info("Got result - " + result);
}
}
事例
for (String recipientId : recipients) {
//判断人员信息是否错在数据中
CompanyEmployee companyEmployee1 = companyEmployeeDao.getCompanyEmployeeById(recipientId);
if (companyEmployee1 == null) {
throw new GlobalException("E0001");
}
TemporaryMissionMemberRela rela = new TemporaryMissionMemberRela();
String relaId = IdUtil.getId() + "";//主键id
rela.setRelaId(relaId);
rela.setCreateTime(date);
rela.setMissionId(temporaryMission.getMissionId());
rela.setMemberId(recipientId);//执行人
rela.setMemberType(1); //执行者
rela.setExecuteStatus(0);//默认未完成
relaDao.insertTemporaryMissionMemberRela(rela);
//app消息推送 完成发布任务消息推送
SendMessage sendMessage = new SendMessage();
sendMessage.setSendContent("您有一个新的待办事项,请及时处理");
sendMessage.setSendTitle("管理后台/物管人员添加待办事项");
sendMessage.setSendType("");
sendMessage.setSendNo("");
sendMessage.setSendId(temporaryMission.getMissionId()+"");
//根据用户id查询用户手机号
try {
MsgPush.push(companyEmployee1.getPhoneNum()+"", GsonUtil.object2Gson(sendMessage));
} catch (Exception e) {
e.printStackTrace();
}
}