unipush + springboot 完成消息推送
- uniapp 开通 unipush 推送服务
- springboot 整合 unipush的 sdk
- springboot 调用相关的api 完成消息推送
开通 unipush
1. 开通服务在应用设置中开启 unipush1.0
这里只展示 uniPush 1.0
原因:
unipush 1.0 :可以用个推服务端api推送
unipush 2.0:不支持用个推服务端api推送,只能用dcloud提供的云函数
unipush 1.0 文档:https://uniapp.dcloud.net.cn/unipush-v1.html
2. 制作自定义的基座 (打包成 Android 应用)
4 申请证书
这里提示需要再开发者后台给打包的应用的开启 开通 unipush 服务
开发者后台:https://dev.dcloud.net.cn/pages/app/list
在开通之前向申请一个安卓证书先
网址:https://www.yunedit.com/
在开发者管理后台给应用开通 unipush1.0
提交完成就去给应用 开通 unipush1.0
点击开通应用就完成了 unipush1.0 的开通了
开通完成之后就可以打包应用了
确保是自定义基座和传统打包
点击打包即可。
运行到 安卓手机上
springboot 集成 unipush SDK
导入坐标:
<!-- 消息推送 -->
<dependency>
<groupId>com.getui.push</groupId>
<artifactId>restful-sdk</artifactId>
<version>1.0.4.0</version>
</dependency>
做配置:
# 消息推送
push:
app_id: xxxxxxxxxx
app_key: xxxxxx
master_secret: xxxxxxx
这些配置从开发者后台中获取
定义配置 push配置类
import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.GtApiConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 个推推送类
*/
@Configuration
public class PushConfig {
@Value("${push.app_id}")
private String appId;
@Value("${push.app_key}")
private String appKey;
@Value("${push.master_secret}")
private String masterSecret;
@Bean(name = "myApiHelper")
public ApiHelper apiHelper() {
// 设置httpClient最大连接数,当并发较大时建议调大此参数。或者启动参数加上 -Dhttp.maxConnections=200
System.setProperty("http.maxConnections", "200");
GtApiConfiguration apiConfiguration = new GtApiConfiguration();
// 填写应用配置
apiConfiguration.setAppId(appId);
apiConfiguration.setAppKey(appKey);
apiConfiguration.setMasterSecret(masterSecret);
// 接口调用前缀,请查看文档: 接口调用规范 -> 接口前缀, 可不填写appId
// 默认为https://restapi.getui.com/v2
apiConfiguration.setDomain("https://restapi.getui.com/v2/");
// 实例化ApiHelper对象,用于创建接口对象
ApiHelper apiHelper = ApiHelper.build(apiConfiguration);
return apiHelper;
}
}
创建工具类 [ 非必须的 ]
import com.alibaba.druid.support.json.JSONUtils;
import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.Settings;
import com.getui.push.v2.sdk.dto.req.Strategy;
import com.getui.push.v2.sdk.dto.req.message.PushChannel;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.AndroidDTO;
import com.getui.push.v2.sdk.dto.req.message.android.ThirdNotification;
import com.getui.push.v2.sdk.dto.req.message.android.Ups;
import com.getui.push.v2.sdk.dto.req.message.ios.Alert;
import com.getui.push.v2.sdk.dto.req.message.ios.Aps;
import com.getui.push.v2.sdk.dto.req.message.ios.IosDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Component
public class PushMsgUtils {
// 消息推送
public static final String MESSAGE_PUSH = "1";
// 离线推送
public static final String OFFLINE_PUSH = "2";
@Resource(name = "myApiHelper")
private ApiHelper myApiHelper;
/**
* 消息推送(离线推送)单cid推送
* cid 必填 设备
* title 必填 标题
* content 必填 内容
* linkUrl 必填 跳转链接
* type 必填 1:消息推送 2:离线推送
*/
public ApiResult<Map<String, Map<String, String>>> pushToSingleByCid(String cId ,
String title,
String content,
String type ) {
PushDTO<Audience> pushDTO = this.buildPushDTO(title, content, type);
// 设置接收人信息
Audience audience = new Audience();
pushDTO.setAudience(audience);
audience.addCid(cId);
// 进行cid单推
PushApi pushApi = myApiHelper.creatApi(PushApi.class);
ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
if (apiResult.isSuccess()) {
// success
log.info("推送成功");
System.out.println(apiResult.getData());
} else {
// failed
log.error("推送失败");
System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
}
return apiResult;
}
/**
* 消息推送(离线推送)单cid推送
* cid 必填 设备
* title 必填 标题
* content 必填 内容
* linkUrl 必填 跳转链接
* type 必填 1:消息推送 2:离线推送
*/
public ApiResult<Map<String, Map<String, String>>> pushToSingleByCIds(List<String> cIds ,
String title,
String content,
String type ) {
PushDTO<Audience> pushDTO = this.buildPushDTO(title, content, type);
// 设置接收人信息
Audience audience = new Audience();
pushDTO.setAudience(audience);
audience.setCid(cIds);
// 进行cid单推
PushApi pushApi = myApiHelper.creatApi(PushApi.class);
ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
if (apiResult.isSuccess()) {
// success
log.info("推送成功");
System.out.println(apiResult.getData());
} else {
// failed
log.error("推送失败");
System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
}
return apiResult;
}
/**
* 消息参数模板
*/
private PushDTO<Audience> buildPushDTO(String title, String content, String type) {
PushDTO<Audience> pushDTO = new PushDTO<>();
// 设置推送参数
//requestid需要每次变化唯一
pushDTO.setRequestId(System.currentTimeMillis() + "");
pushDTO.setGroupName("wxb-group");
//配置推送条件
// 1: 表示该消息在用户在线时推送个推通道,用户离线时推送厂商通道;
// 2: 表示该消息只通过厂商通道策略下发,不考虑用户是否在线;
// 3: 表示该消息只通过个推通道下发,不考虑用户是否在线;
// 4: 表示该消息优先从厂商通道下发,若消息内容在厂商通道代发失败后会从个推通道下发。
Strategy strategy = new Strategy();
strategy.setDef(1);
strategy.setSt(1);
Settings settings = new Settings();
settings.setStrategy(strategy);
pushDTO.setSettings(settings);
//消息有效期,走厂商消息需要设置该值
settings.setTtl(3600000);
PushChannel pushChannel = new PushChannel();
Map<String, String> map = new HashMap<>();
map.put("title", title);
map.put("content", content);
map.put("type", type);
// 转json对象
String payload = JSONUtils.toJSONString(map);
// ========================= ios离线配置 ==============================
//推送苹果离线通知标题内容
Alert alert = new Alert();
//苹果离线通知栏标题
alert.setTitle(title);
//苹果离线通知栏内容
alert.setBody(content);
Aps aps = new Aps();
//1表示静默推送(无通知栏消息),静默推送时不需要填写其他参数。
// 苹果建议1小时最多推送3条静默消息
aps.setContentAvailable(0);
aps.setSound("default");
aps.setAlert(alert);
IosDTO iosDTO = new IosDTO();
iosDTO.setPayload(payload);
iosDTO.setAps(aps);
iosDTO.setType("notify");
pushChannel.setIos(iosDTO);
// =================== 安卓离线厂商通道推送消息体 ===========================
AndroidDTO androidDTO = new AndroidDTO();
pushDTO.setPushChannel(pushChannel);
pushChannel.setAndroid(androidDTO);
Ups ups = new Ups();
androidDTO.setUps(ups);
ThirdNotification notification1 = new ThirdNotification();
ups.setNotification(notification1);
//安卓离线展示的标题
notification1.setTitle(title);
//安卓离线展示的内容
notification1.setBody(content);
notification1.setClickType("intent");
notification1.setIntent("intent:#Intent;action=android.intent.action.oppopush;" +
"launchFlags=0x14000000;" +
"component=包名/io.dcloud.PandoraEntry;S.UP-OL-SU=true;" +
"S.title=" + title + ";" +
"S.content="+ content + ";" +
"S.payload=" + payload + ";end");
// notification1.setPayload(payload);
//各厂商自有功能单项设置
// ups.addOption("HW", "/message/android/notification/badge/class", "io.dcloud.PandoraEntry ");
// ups.addOption("HW", "/message/android/notification/badge/add_num", 1);
// ups.addOption("HW", "/message/android/notification/importance", "HIGH");
// ups.addOption("VV","classification",1);
//设置options 方式一
// ups.addOption("HW","badgeAddNum",3);
// ups.addOption("HW","badgeClass","com.getui.demo.GetuiSdkDemoActivity");
// ups.addOption("OP","app_message_id",11);
// ups.addOption("VV","message_sort",1);
// ups.addOptionAll("channel","default");
// PushMessage在线走个推通道才会起作用的消息体
PushMessage pushMessage = new PushMessage();
pushDTO.setPushMessage(pushMessage);
Map<String, Object> mapTC = new HashMap<>();
mapTC.put("title", title);
mapTC.put("content", content);
mapTC.put("payload", map);
String jsonTC = JSONUtils.toJSONString(mapTC);
pushMessage.setTransmission(jsonTC);
log.info("pushDTO:{}", pushDTO);
return pushDTO;
}
/**
* 离线通知消息参数模板
*
*/
private PushDTO<Audience> offlinePushDTO(String title, String content, String linkUrl, String type) {
PushDTO<Audience> pushDTO = new PushDTO<>();
// 设置推送参数
//requestid需要每次变化唯一
pushDTO.setRequestId(System.currentTimeMillis() + "");
pushDTO.setGroupName("wxb-group");
// PushMessage在线走个推通道才会起作用的消息体
Map<String, String> map = new HashMap<>();
map.put("title", title);
map.put("content", content);
map.put("linkUrl", linkUrl);
map.put("type", type);
PushMessage pushMessage = new PushMessage();
pushDTO.setPushMessage(pushMessage);
Map<String, Object> mapTC = new HashMap<>();
mapTC.put("title", title);
mapTC.put("content", content);
mapTC.put("payload", map);
String jsonTC = JSONUtils.toJSONString(mapTC);
System.err.println(jsonTC);
pushMessage.setTransmission(jsonTC);
log.info("pushDTO:{}", pushDTO);
return pushDTO;
}
}
这里演示单推送:步骤
- 获取 cId
在 vue.app 中
<script>
export default {
onLaunch: function() {
console.log('App Launch')
// 获取 cId 可以理解为设备id
uni.getPushClientId({
success: (res) => {
console.log(res.cid);
},
fail: (res) => {
console.log(res);
}
})
},
onShow: function() {
console.log('App Show')
},
onHide: function() {
console.log('App Hide')
}
}
</script>
<style>
/*每个页面公共css */
</style>
- 调用工具类推送消息
@Test
public void pushMsg() {
String cid = "9d02a626d057cac8b7a5a5ff84bde125";
String title = "测试 - 后端";
String content = "测试 - 后端 - 内容";
String type = PushMsgUtils.MESSAGE_PUSH;
ApiResult<Map<String, Map<String, String>>> mapApiResult = pushMsgUtils.pushToSingleByCid(cid, title, content, type);
System.out.println("mapApiResult = " + mapApiResult);
}