1.添加依赖
<!--极光推送-->
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.2.9</version>
</dependency>
2.添加配置类
JPushConfig
import cn.jpush.api.JPushClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
@ConfigurationProperties
public class JPushConfig {
// 极光账号上的 AppKey
@Value("7fd04ce01c973d1ad703bcb4")
private String appkey;
// 极光账号上的 Master Secret
@Value("a8414209858d8037654258d0")
private String secret;
private JPushClient jPushClient;
/**
* 推送客户端
* @return
*/
@PostConstruct
public void initJPushClient() {
jPushClient = new JPushClient(secret, appkey);
}
/**
* 获取推送客户端
* @return
*/
public JPushClient getJPushClient() {
return jPushClient;
}
}
3.推送实体
PushBean
import lombok.Data;
import java.util.Map;
@Data
public class PushBean {
// 必填, 通知内容, 内容可以为空字符串,则表示不展示到通知栏。
private String alert;
// 可选, 附加信息, 供业务使用。
private Map<String, String> extras;
//android专用 可选, 通知标题 如果指定了,则通知里原来展示 App名称的地方,将展示成这个字段。
private String title;
}
4.封装第三方API
IJPushService
import cn.jpush.api.push.model.PushPayload;
/**
* 极光推送
*
* 封装第三方API
*/
public interface IJPushService {
boolean pushAll(PushBean pushBean);
boolean pushIos(PushBean pushBean);
boolean pushIos(PushBean pushBean, String... registids);
boolean pushAndroid(PushBean pushBean);
boolean pushAndroid(PushBean pushBean, String... registids);
boolean sendPush(PushPayload pushPayload);
}
JPushServiceImpl
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
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.Notification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 极光推送
*
* 封装第三方API
*/
@Service
@Slf4j
public class JPushServiceImpl implements IJPushService {
@Autowired
private JPushConfig jPushConfig;
/**
* 广播 (所有平台,所有设备, 不支持附加信息)
* @param pushBean 推送内容
* @return
*/
@Override
public boolean pushAll(PushBean pushBean){
return sendPush(PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.all())
.setNotification(Notification.alert(pushBean.getAlert()))
.build());
}
/**
* ios广播
* @param pushBean 推送内容
* @return
*/
@Override
public boolean pushIos(PushBean pushBean){
return sendPush(PushPayload.newBuilder()
.setPlatform(Platform.ios())
.setAudience(Audience.all())
.setNotification(Notification.ios(pushBean.getAlert(), pushBean.getExtras()))
.build());
}
/**
* ios通过registid推送 (一次推送最多 1000 个)
* @param pushBean 推送内容
* @param registids 推送id
* @return
*/
@Override
public boolean pushIos(PushBean pushBean, String... registids){
return sendPush(PushPayload.newBuilder()
.setPlatform(Platform.ios())
.setAudience(Audience.registrationId(registids))
.setNotification(Notification.ios(pushBean.getAlert(), pushBean.getExtras()))
.build());
}
/**
* android广播
* @param pushBean 推送内容
* @return
*/
@Override
public boolean pushAndroid(PushBean pushBean){
return sendPush(PushPayload.newBuilder()
.setPlatform(Platform.android())
.setAudience(Audience.all())
.setNotification(Notification.android(pushBean.getAlert(), pushBean.getTitle(), pushBean.getExtras()))
.build());
}
/**
* android通过registid推送 (一次推送最多 1000 个)
* @param pushBean 推送内容
* @param registids 推送id
* @return
*/
@Override
public boolean pushAndroid(PushBean pushBean, String ... registids){
return sendPush(PushPayload.newBuilder()
.setPlatform(Platform.android())
.setAudience(Audience.registrationId(registids))
.setNotification(Notification.android(pushBean.getAlert(), pushBean.getTitle(), pushBean.getExtras()))
.build());
}
/**
* 调用api推送
* @param pushPayload 推送实体
* @return
*/
@Override
public boolean sendPush(PushPayload pushPayload){
log.info("发送极光推送请求: {}", pushPayload);
PushResult result = null;
try{
result = jPushConfig.getJPushClient().sendPush(pushPayload);
} catch (APIConnectionException e) {
log.error("极光推送连接异常: ", e);
} catch (APIRequestException e) {
log.error("极光推送请求异常: ", e);
}
if (result!=null && result.isResultOK()) {
log.info("极光推送请求成功: {}", result);
return true;
}else {
log.info("极光推送请求失败: {}", result);
return false;
}
}
}
5.封装业务功能
IMyJPushService
/**
* 推送服务
*
* 封装业务功能
*/
public interface IMyJPushService {
boolean pushAll(PushBean pushBean);
boolean pushIos(PushBean pushBean);
boolean pushIos(PushBean pushBean, String... registids);
boolean pushAndroid(PushBean pushBean);
boolean pushAndroid(PushBean pushBean, String... registids);
String[] checkRegistids(String[] registids);
}
MyJPushServiceImpl
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 推送服务
*
* 封装业务功能
*/
@Service
public class MyJPushServiceImpl implements IMyJPushService {
/** 一次推送最大数量 (极光限制1000) */
private static final int max_size = 800;
@Autowired
private IJPushService jPushService;
/**
* 推送全部, 不支持附加信息
* @return
*/
@Override
public boolean pushAll(PushBean pushBean){
return jPushService.pushAll(pushBean);
}
/**
* 推送全部ios
* @return
*/
@Override
public boolean pushIos(PushBean pushBean){
return jPushService.pushIos(pushBean);
}
/**
* 推送ios 指定id
* @return
*/
@Override
public boolean pushIos(PushBean pushBean, String... registids){
registids = checkRegistids(registids); // 剔除无效registed
while (registids.length > max_size) { // 每次推送max_size个
jPushService.pushIos(pushBean, Arrays.copyOfRange(registids, 0, max_size));
registids = Arrays.copyOfRange(registids, max_size, registids.length);
}
return jPushService.pushIos(pushBean, registids);
}
/**
* 推送全部android
* @return
*/
@Override
public boolean pushAndroid(PushBean pushBean){
return jPushService.pushAndroid(pushBean);
}
/**
* 推送android 指定id
* @return
*/
@Override
public boolean pushAndroid(PushBean pushBean, String... registids){
registids = checkRegistids(registids); // 剔除无效registed
while (registids.length > max_size) { // 每次推送max_size个
jPushService.pushAndroid(pushBean, Arrays.copyOfRange(registids, 0, max_size));
registids = Arrays.copyOfRange(registids, max_size, registids.length);
}
return jPushService.pushAndroid(pushBean, registids);
}
/**
* 剔除无效registed
* @param registids
* @return
*/
@Override
public String[] checkRegistids(String[] registids) {
List<String> regList = new ArrayList<String>(registids.length);
for (String registid : registids) {
if (registid!=null && !"".equals(registid.trim())) {
regList.add(registid);
}
}
return regList.toArray(new String[0]);
}
}
6.测试 - 广播
JPushController
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class JPushController {
@Autowired
private IMyJPushService service;
/**
* 广播
* @param title
* @param content
* @param registids 指定id
* @return
*/
@PostMapping("/jpush")
@ResponseBody
public boolean test(String title, String content, String registids){
PushBean pushBean = new PushBean();
pushBean.setTitle(title);
pushBean.setAlert(content);
return service.pushIos(pushBean);
}
}
发送给所有ios用户
温馨提示: 指定id可以发给某个ios用户, android的道理也是一样, 我这里就不一 一演示了。大家动起手来实战一下...