一、准备工作
1、进入【服务中心】-【开发者平台】
2、【创建应用】,填写应用名称和图标(填写项目名称,项目logo就行,也可填写其他的)
3、选择【消息推送】服务,点击下一步
Demo测试
参照文档:uni-app 推送官方插件集成指南 · BDS技术支持组
注:本地真机测试需要制作自定义基座才可以测试
安卓证书获取方式:打开命令控制台 输入 keytool -genkey -alias Android(包别名) -keyalg RSA -keysize 2048 -validity 36500(证书有效天数) -keystore certificate(证书名称).keystore
示例:
ios需要苹果开发者账号制作证书:https://ask.dcloud.net.cn/article/152
测试:极光推送控制台-》通知消息
二、Postman 模拟后端Server主动推送消息
参考资料:http://https/docs.jiguang.cn/jpush/server/push/rest_api_v3_push
{"platform": "all","audience" : {"registration_id" : [ "指定registration_id"]},"notification": {"alert": "Hello, {{content}}!"},"message": {"msg_content": "Hi,JPush","content_type": "text","title": "msg","extras": {"key": "value"}}
}
发送成功截图
服务端集成:服务端 SDK - 极光文档
参考资料:
1、服务端 SDK - 极光文档
2、【SpringBoot】在SpringBoot中如何使用 极光推送_springboot 极光推送-CSDN博客
三、服务端集成本地测试示例
本地测试项目 前后端分离框架,完整代码如下
POM依赖
<dependency><groupId>cn.jpush.api</groupId><artifactId>jiguang-common</artifactId><version>1.1.4</version>
</dependency>
<dependency><groupId>cn.jpush.api</groupId><artifactId>jpush-client</artifactId><version>3.3.10</version>
</dependency>
Config配置
@Configuration
public class JiGuangConfig {/*** 极光官网-个人管理中心-appkey* https://www.jiguang.cn/*/
// @Value("${jpush.appkey}")private String appkey ="xxxxxxxxxxxxx";/*** 极光官网-个人管理中心-点击查看-secret*/
// @Value("${jpush.secret}")private String secret = "xxxxxxxxxxxxxxxxxx";private JPushClient jPushClient;/*** 推送客户端* @return*/@PostConstructpublic void initJPushClient() {jPushClient = new JPushClient(secret, appkey);}/*** 获取推送客户端* @return*/public JPushClient getJPushClient() {return jPushClient;}
}
Controller
@RestController
@RequestMapping("/ctl/jgPush")
public class JgPushController extends BaseController {@Autowiredprivate JiGuangPushService jiGuangService;@PostMapping("/jgTest")public void jgTest(){//定义和赋值推送实体PushBean pushBean = new PushBean();pushBean.setTitle("标题");pushBean.setAlert("测试消息");//额外推送信息Map<String,String> map = new HashMap<>();map.put("userName","张三");pushBean.setExtras(map);//进行推送,推送到指定Android客户端的用户,返回推送结果布尔值String [] rids = new String[1];rids[0] = "xxxxxxxxxxx";//指定idboolean flag = jiGuangService.pushAndroid(pushBean,rids);}
}
Service
public interface JiGuangPushService {/*** 广播 (所有平台,所有设备, 不支持附加信息)* @return*/public boolean pushAll(PushBean pushBean);/*** 推送全部ios ios广播* @return*/public boolean pushIos(PushBean pushBean);/*** 推送ios 指定id* @return*/public boolean pushIos(PushBean pushBean, String... registids);/*** 推送全部android* @return*/public boolean pushAndroid(PushBean pushBean);/*** 推送android 指定id* @return*/public boolean pushAndroid(PushBean pushBean, String... registids);/*** 剔除无效registed* @param registids* @return*/public String[] checkRegistids(String[] registids);/*** 调用api推送* @param pushPayload 推送实体* @return*/public boolean sendPush(PushPayload pushPayload);
}
Service实现
@Service
public class JiGuangPushServiceImpl implements JiGuangPushService {private static final Logger log = LoggerFactory.getLogger(JiGuangPushServiceImpl.class);/** 一次推送最大数量 (极光限制1000) */private static final int max_size = 800;@Autowiredprivate JiGuangConfig jPushConfig;/*** 广播 (所有平台,所有设备, 不支持附加信息)* @return*/@Overridepublic boolean pushAll(PushBean pushBean){return sendPush(PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.all()).setNotification(Notification.alert(pushBean.getAlert())).build());}/*** 推送全部ios ios广播* @return*/@Overridepublic boolean pushIos(PushBean pushBean){return sendPush(PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.all()).setNotification(Notification.ios(pushBean.getAlert(), pushBean.getExtras())).build());}/*** 推送ios 指定id* @return*/@Overridepublic boolean pushIos(PushBean pushBean, String... registids){registids = checkRegistids(registids); // 剔除无效registedwhile (registids.length > max_size) { // 每次推送max_size个sendPush(PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.registrationId(Arrays.copyOfRange(registids, 0, max_size))).setNotification(Notification.ios(pushBean.getAlert(), pushBean.getExtras())).build());registids = Arrays.copyOfRange(registids, max_size, registids.length);}return sendPush(PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.registrationId(Arrays.copyOfRange(registids, 0, max_size))).setNotification(Notification.ios(pushBean.getAlert(), pushBean.getExtras())).build());}/*** 推送全部android* @return*/@Overridepublic 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 指定id* @return*/@Overridepublic boolean pushAndroid(PushBean pushBean, String... registids){registids = checkRegistids(registids); // 剔除无效registedwhile (registids.length > max_size) { // 每次推送max_size个sendPush(PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.registrationId(Arrays.copyOfRange(registids, 0, max_size))).setNotification(Notification.android(pushBean.getAlert(), pushBean.getTitle(), pushBean.getExtras())).build());registids = Arrays.copyOfRange(registids, max_size, registids.length);}return sendPush(PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.registrationId(registids)).setNotification(Notification.android(pushBean.getAlert(), pushBean.getTitle(), pushBean.getExtras())).build());}/*** 剔除无效registed* @param registids* @return*/@Overridepublic 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]);}/*** 调用api推送* @param pushPayload 推送实体* @return*/@Overridepublic boolean sendPush(PushPayload 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;}}
}
Bean实体类
public class PushBean {// 必填, 通知内容, 内容可以为空字符串,则表示不展示到通知栏。private String alert;// 可选, 附加信息, 供业务使用。private Map<String, String> extras;//android专用// 可选, 通知标题 如果指定了,则通知里原来展示 App名称的地方,将展示成这个字段。private String title;public String getAlert() {return alert;}public void setAlert(String alert) {this.alert = alert;}public Map<String, String> getExtras() {return extras;}public void setExtras(Map<String, String> extras) {this.extras = extras;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public PushBean() {}public PushBean(String alert, Map<String, String> extras, String title) {this.alert = alert;this.extras = extras;this.title = title;}@Overridepublic String toString() {return "PushBean{" +"alert='" + alert + '\'' +", extras=" + extras +", title='" + title + '\'' +'}';}
}
调用此接口成功如下图