展开
一. 极光推送代码
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.2.9</version>
</dependency>
package com.jinzhi.jpush.config;
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 JiGuangConfig {
// 极光官网-个人管理中心-appkey
@Value("${push.appkey}")
private String appkey;
// 极光官网-个人管理中心-点击查看-secret
@Value("${push.secret}")
private String secret;
private JPushClient jPushClient;
// 推送客户端
@PostConstruct
public void initJPushClient() {
jPushClient = new JPushClient(secret, appkey);
}
// 获取推送客户端
public JPushClient getJPushClient() {
return jPushClient;
}
}
package com.jinzhi.jpush.model;
import lombok.Data;
import java.util.Map;
@Data
public class PushBean {
// 必填, 通知内容, 内容可以为空字符串,则表示不展示到通知栏。
private String alert;
// 可选, 附加信息, 供业务使用。
private Map<String, String> extras;
//android专用// 可选, 通知标题,如果指定了,则通知里原来展示 App名称的地方,将展示成这个字段。
private String title;
}
package com.jinzhi.jpush.service;
import cn.jpush.api.push.model.PushPayload;
import com.jinzhi.jpush.model.PushBean;
/**
* 极光推送
* 封装第三方api相关
*/
public interface MyJiGuangPushService {
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);
}
package com.jinzhi.jpush.service;
import com.jinzhi.jpush.model.PushBean;
/**
* 推送服务
* 封装业务功能相关
*/
public interface JiGuangPushService {
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);
}
package com.jinzhi.jpush.service.impl;
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 com.jinzhi.jpush.config.JiGuangConfig;
import com.jinzhi.jpush.model.PushBean;
import com.jinzhi.jpush.service.MyJiGuangPushService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 极光推送
* 封装第三方api相关
*/
@Service
@Slf4j
public class MyJiGuangPushServiceImpl implements MyJiGuangPushService {
@Autowired
private JiGuangConfig 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;
}
}
}
package com.jinzhi.jpush.service.impl;
import com.jinzhi.jpush.model.PushBean;
import com.jinzhi.jpush.service.JiGuangPushService;
import com.jinzhi.jpush.service.MyJiGuangPushService;
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 JiGuangPushServiceImpl implements JiGuangPushService {
/** 一次推送最大数量 (极光限制1000) */
private static final int max_size = 800;
@Autowired
private MyJiGuangPushService 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]);
}
}
二. 测试推送
首先需要去极光推送官网去创建应用,这里不做过多说明。
首先需要我们的系统中,每个用户都对应一个regId,即唯一极光ID。
@Autowired
private JiGuangPushService service;
/**
* 群推,广播
* @param title 推送标题
* @param content 推送内容
* @return
*/
@PostMapping("/pushAll")
public Result pushAll(@RequestParam String title, @RequestParam String content){
PushBean pushBean = new PushBean();
pushBean.setTitle(title);
pushBean.setAlert(content);
boolean flag = jiGuangPushService.pushAndroid(pushBean);
return ResultUtil.success(flag);
}
发送请求,所有客户端都会接到推送消息:
/**
* 单独对regId进行推送
* @param title 推送标题
* @param regId 设备对应的唯一极光ID
* @param content 推送内容
* @return
*/
@PostMapping("/push")
public Result push(@RequestParam String title,@RequestParam String regId,@RequestParam String content){
PushBean pushBean = new PushBean();
pushBean.setTitle(title);
pushBean.setAlert(content);
boolean flag = jiGuangPushService.pushAndroid(pushBean, regId);
return ResultUtil.success(flag);
}
发送请求,可以看到只有指定的设备号才接收到消息:
亲测可用
————————————————
版权声明:本文为CSDN博主「AkiraNicky」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/akiranicky/article/details/91442008