需要先去极光申请注册极光IM,获取appkey和masterSecret
https://docs.jiguang.cn/jmessage/server/rest_api_im/
http://jpush.github.io/jmessage-api-java-client/apidocs/
https://github.com/jpush/jmessage-api-java-client
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>wnews</artifactId> <groupId>com.fd.wnews</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>fd-wnews-common</artifactId> <dependencies><!-- <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.2.Final</version> </dependency>--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-support</artifactId> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-transport</artifactId> </dependency> <dependency> <groupId>com.fd.businessengine</groupId> <artifactId>be-common-entity</artifactId> <version>1.0.2-SNAPSHOT</version> <scope>compile</scope> </dependency> <dependency> <groupId>cn.jpush.api</groupId> <artifactId>jpush-client</artifactId> <version>3.2.17</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.5.9</version> </dependency> <dependency> <groupId>com.arronlong</groupId> <artifactId>httpclientutil</artifactId> <version>1.0.4</version> </dependency> <dependency> <groupId>cn.jpush.api</groupId> <artifactId>jiguang-common</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>backport-util-concurrent</groupId> <artifactId>backport-util-concurrent</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>cn.jpush.api</groupId> <artifactId>jmessage-client</artifactId> <version>1.1.8</version> </dependency> </dependencies> </project>
工具类
package com.fd.wnews.utils.jiguang; import cn.jiguang.common.resp.APIConnectionException; import cn.jiguang.common.resp.APIRequestException; import cn.jpush.api.JPushClient; import cn.jpush.api.push.PushResult; import cn.jpush.api.push.model.Message; 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 java.util.Map; /** * @Description 极光推送工具类 * @Author lc * @Date 2019-11-20 上午 11:20 */ public class PushUtil { public static final String APP_KEY = "*************"; public static final String MASTER_SECRET = "*************"; /** * @param param * @return cn.jpush.api.push.PushResult * @Description Android * @Author lc * @Date 2019-11-20 上午 11:34 **/ public static PushResult jpushAndroid(Map<String, String> param) { //创建JPushClient(极光推送的实例) JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY); //推送的关键,构造一个payload PushPayload payload = PushPayload.newBuilder() .setPlatform(Platform.android())//指定android平台的用户 .setAudience(Audience.all())//你项目中的所有用户 //.setAudience(Audience.registrationId(param.get("id")))//registrationId指定用户 .setNotification(Notification.android(param.get("msg"), "这是title", param)) //发送内容 .setOptions(Options.newBuilder().setApnsProduction(false).build()) //这里是指定开发环境,不用设置也没关系 .setMessage(Message.content(param.get("msg")))//自定义信息 .build(); PushResult pu = null; try { pu = jpushClient.sendPush(payload); } catch (APIConnectionException e) { e.printStackTrace(); } catch (APIRequestException e) { e.printStackTrace(); } return pu; } /** * @param param * @return cn.jpush.api.push.PushResult * @Description ios * @Author lc * @Date 2019-11-20 上午 11:34 **/ public static PushResult jpushIOS(Map<String, String> param) { //创建JPushClient JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY); PushPayload payload = PushPayload.newBuilder() .setPlatform(Platform.ios())//ios平台的用户 .setAudience(Audience.all())//所有用户 //.setAudience(Audience.registrationId(param.get("id")))//registrationId指定用户 .setNotification(Notification.newBuilder() .addPlatformNotification(IosNotification.newBuilder() .setAlert(param.get("msg")) .setBadge(+1) .setSound("happy")//这里是设置提示音(更多可以去官网看看) .addExtras(param) .build()) .build()) .setOptions(Options.newBuilder().setApnsProduction(false).build()) .setMessage(Message.newBuilder().setMsgContent(param.get("msg")).addExtras(param).build())//自定义信息 .build(); PushResult pu = null; try { pu = jpushClient.sendPush(payload); } catch (APIConnectionException e) { e.printStackTrace(); } catch (APIRequestException e) { e.printStackTrace(); } return pu; } /** * @param param * @return void * @Description All所有平台 * @Author lc * @Date 2019-11-20 上午 11:26 **/ public static PushResult jpushAll(Map<String, String> param) { //创建JPushClient JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY); //创建option PushPayload payload = PushPayload.newBuilder() .setPlatform(Platform.all()) //所有平台的用户 .setAudience(Audience.registrationId(param.get("id")))//registrationId指定用户 .setNotification(Notification.newBuilder() .addPlatformNotification(IosNotification.newBuilder() //发送ios .setAlert(param.get("msg")) //消息体 .setBadge(+1) .setSound("happy") //ios提示音 .addExtras(param) //附加参数 .build()) .addPlatformNotification(AndroidNotification.newBuilder() //发送android .addExtras(param) //附加参数 .setAlert(param.get("msg")) //消息体 .build()) .build()) .setOptions(Options.newBuilder().setApnsProduction(true).build())//指定开发环境 true为生产模式 false 为测试模式 (android不区分模式,ios区分模式) .setMessage(Message.newBuilder().setMsgContent(param.get("msg")).addExtras(param).build())//自定义信息 .build(); PushResult pu = null; try { pu = jpushClient.sendPush(payload); System.out.println(pu.toString()); } catch (APIConnectionException e) { e.printStackTrace(); } catch (APIRequestException e) { e.printStackTrace(); } return pu; } }