极光推送工具类
官方网址:
https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#_20
上代码:
public static void jpushAll(Map<String, String> parm) {
//创建JPushClient
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
//创建option
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.all()) //所有平台的用户Android, iOS, Windows Phone
.setAudience(Audience.alias(parm.get("id")))//推送目标,向谁推送
.setNotification(Notification.newBuilder()//通知
//发送ios
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(parm.get("msg")) //消息体
.setBadge(+1)
.setSound("happy") //ios提示音,如果识别不到,它会自己添加默认的
.addExtras(parm) //附加参数,自己定义的
.build())
//发送android
.addPlatformNotification(AndroidNotification.newBuilder()
.setAlert(parm.get("msg")) //消息体
.addExtras(parm) //附加参数,自己定义的
.build())
.build())
.setOptions(Options.newBuilder().setApnsProduction(true).build())//指定开发环境 true为生产模式 false 为测试模式 (android不区分模式,ios区分模式)
.build();
try {
PushResult pu = jpushClient.sendPush(payload);
System.out.println(pu.toString());
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
}
pom.xml:
<!-- 极光推送 https://mvnrepository.com/artifact/cn.jpush.api/jpush-client -->
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.3.11</version>
</dependency>
注释:
前端已经集成了极光推送,APP_KEY和MASTER_SECRET是此APP极光推送的key和密钥。