原文链接:
https://www.cnblogs.com/yueguanguanyun/p/8485381.html
所需jar包,在maven中添加下列依赖:
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.2.17</version>
</dependency>
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jiguang-common</artifactId>
<version>1.0.3</version>
</dependency>
<!--以上部分为极光推送所依赖的jar包-->
只实现简单的向所有的Android 和 IOS发送推送:
public class Jdpush {
//极光推送>>Android
//Map<String, String> parm是我自己传过来的参数,同学们可以自定义参数
public static void jpushAndroid(Map<String, String> parm) {
// 设置好账号的app_key和masterSecret
String appKey = "9ceb4487959dce33b91f43ce";
String masterSecret = "f9784c5647edb38ca216ce55";
//创建JPushClient
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
//推送的关键,构造一个payload
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.android())//指定android平台的用户
.setAudience(Audience.all())//你项目中的所有用户
.setNotification(Notification.android(parm.get("msg"), "日报", parm))
//发送内容,这里不要盲目复制粘贴,这里是我从controller层中拿过来的参数)
.setOptions(Options.newBuilder().setApnsProduction(false).build())
//这里是指定开发环境,不用设置也没关系
.setMessage(Message.content(parm.get("msg")))//自定义信息
.build();
try {
PushResult pu = jpushClient.sendPush(payload);
System.out.println(DateUtil.getSimpleDate() + "发送推送完成");
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
}
//极光推送>>ios
//Map<String, String> parm是我自己传过来的参数,同学们可以自定义参数
public static void jpushIOS(Map<String, String> parm) {
// 设置好账号的app_key和masterSecret是必须的
String appKey = "*********************";
String masterSecret = "**********************";
//创建JPushClient
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.ios())//ios平台的用户
.setAudience(Audience.all())//所有用户
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(parm.get("msg"))
.setBadge(+1)
.setSound("happy")
.addExtras(parm)
.build())
.build())
.setOptions(Options.newBuilder().setApnsProduction(false).build())
.setMessage(Message.newBuilder().setMsgContent(parm.get("msg")).addExtras(parm).build())//自定义信息
.build();
try {
PushResult pu = jpushClient.sendPush(payload);
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
}
}
Controller类:
/**
* 推送
*/
/**
* 定时任务
*/
@Configuration
@EnableScheduling
@Controller
public class SMSController{
//这里的定时任务,因为我和 Android 打交道比较少,所以没有打算深究极光推送自己的定时任务,只是调用的Java的定时注解
@Scheduled(cron = "0 00 19 ? * *") //此注解的意思是每天下午7点钟发送推送
public static void test(){
SMSController sms = new SMSController();
try {
// //无所谓 //标题 //内容
sms.addArticle("11", "日报", "今天的工作量已统计");
}catch(Exception e){
e.printStackTrace();
}
}
public Map<String, Object> addArticle(String id, String Atitle, String Msg) throws Exception {
Map<String, Object> result = new HashMap<String, Object>();
//从前端页面传个参数过来判断是否推送
/*if((infoMap.get("is_push").toString()).trim().equals("1")){*/
//设置推送参数
//这里同学们就可以自定义推送参数了
Map<String, String> parm =new HashMap<String, String>();
parm.put("id",id);
parm.put("Atitle",Atitle);
parm.put("msg",Msg);
//调用ios的
//Jdpush.jpushIOS(parm);
//然后调用安卓的
Jdpush.jpushAndroid(parm);
//}
return result;
}
}