极光推送是目前比较实用的一种消息推送,极光推送可以指定推送的平台,今天我们主要来说一下推送所有平台的功能实现
首先需要在极光官网https://www.jiguang.cn/?hmsr=%E5%93%81%E4%B8%933&hmpl=%E6%A0%87%E9%A2%98&hmcu=&hmkw=&hmci= 注册应用的appkey和masterSecret,然后需要去极光官方文档拿到所需要的maven依赖
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.3.4</version>
</dependency>
<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>
拿到这些之后,我们就可以开始编写我们的代码了
首先我们需要建一个类
public class JoushClientUtil {
private final static String appKey = "***********";//这里我们设置为静态常量,方便我们取值
private final static String masterSecret = "*********";//同上
private static JPushClient jpushClient = new JPushClient(masterSecret ,appKey );
//极光推送>>>所有平台
public static void jpushAll(Map<String,String> parm) {
PushPayload payload = PushPayload.newBuilder()
.setPlatform(platform.all()) //所有平台的用户
// .setAudience(Audience.alias(parm.get("id"))) //推送指定用户
.setAudience(Audience.all()) //推送所有用户,广播式推送
.setNotification(Notification.newBuilder() .addPlatformNotification(IosNotification.newBuilder() .setAlert(parm.get("msg")) .setBadge(+1) .setSound("happy") .addExtras(parm) .build()) .addPlatformNotification(AndroidNotification.newBuilder() .addExtras(parm) .setAlert(parm.get("msg")) .build()) .build()) .setOptions(Options.newBuilder().setApnsProduction(false).build())//指定开发环境 false为测试环境 true 生产环境 .setMessage(Message.newBuilder().setMsgContent(parm.get("msg")).addExtras(parm).build())//自定义信息 .build();
public static void maon (String[] args) {
Map<String,String> parm = new HashMap<>();
parm.put("id","指定推送用户的id");
parm.put("title","标题");
parm.put("msg","内容");
JpushClient.jpushAll(parm);
}
}
}
第一次自己写博客,有什么不对的地方希望大家谅解,也可以去极光官网看看