1. 添加poom依赖:
<dependency> <groupId>cn.jpush.api</groupId> <artifactId>jiguang-common</artifactId> <version>1.1.1</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. 创建工具类如下(红色为需要注意的地方):
package com.demo.jiguangpush; 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.IosNotification; import cn.jpush.api.push.model.notification.Notification; import java.util.Map; public class JgPush { // 设置好账号的app_key和masterSecret /*private static String appKey = "3c8473f025736ea1bfa3a1c1"; private static String masterSecret = "ee97e1c77b2e3710da671886";*/ //我的是数据库配置的 //极光推送>>Android //Map<String, String> parm是我自己传过来的参数,可以自定义参数 public static String jpushAndroid(Map<String, String> parm) { //创建JPushClient JPushClient jpushClient = new JPushClient(parm.get("masterSecret"), parm.get("appKey")); //注意参数顺序,不然会报错(masterSecret, appKey) //推送的关键,构造一个payload PushPayload payload = PushPayload.newBuilder() .setPlatform(Platform.android())//指定android平台的用户 .setAudience(Audience.all())//你项目中的所有用户 .setNotification(Notification.android(parm.get("pushContent"), parm.get("pushTitle"), parm)) //发送内容,这里不要盲目复制粘贴,这里是我从controller层中拿过来的参数) .setOptions(Options.newBuilder().setApnsProduction(false).build()) //这里是指定开发环境,不用设置也没关系,android只有一种 .setMessage(Message.content(parm.get("pushContent")))//自定义信息 .build(); try { PushResult pu = jpushClient.sendPush(payload); } catch (APIConnectionException e) { e.printStackTrace(); } catch (APIRequestException e) { e.printStackTrace(); return e.getErrorMessage(); //抛出失败错误信息 } return "success"; } //极光推送>>ios //Map<String, String> parm是我自己传过来的参数,可以自定义参数 public static String jpushIOS(Map<String, String> parm) { //创建JPushClient JPushClient jpushClient = new JPushClient(parm.get("masterSecret"), parm.get("appKey")); PushPayload payload = PushPayload.newBuilder() .setPlatform(Platform.ios())//ios平台的用户 .setAudience(Audience.all())//所有用户 .setNotification(Notification.newBuilder() .addPlatformNotification(IosNotification.newBuilder() .setAlert(parm.get("pushContent")) // .setBadge(+1) //如设置此参数,则app端的角标会是此参数,不加此参数,则极光会自动的实现 +1 的功能 .setSound("default") .addExtras(parm) .build()) .build()) .setOptions(Options.newBuilder().setApnsProduction(true).build()) //false为开发环境;true为生产环境(ios由两种环境,需区别开来) .setMessage(Message.newBuilder().setMsgContent(parm.get("pushContent")).addExtras(parm).build())//自定义信息 .build(); try { PushResult pu = jpushClient.sendPush(payload); } catch (APIConnectionException e) { e.printStackTrace(); } catch (APIRequestException e) { e.printStackTrace(); return e.getErrorMessage(); } return "success"; } }
遇到的坑:需要在app端实现 +1 功能(推送一条信息,没有查看时,又推送了一条,此时,角标应该变为 2)最后的结果是 角标一直是 1,后来找到原因是参数设置问题:
极光推送官网文档:https://docs.jiguang.cn/jpush/server/sdk/java_sdk/
极光推送参考文章:https://blog.csdn.net/qq_35860097/article/details/75117323
原文:https://www.cnblogs.com/mufengforward/p/9289551.html