效果图:
1.安装依赖:
<dependency>
<groupId>com.getui.push</groupId>
<artifactId>restful-sdk</artifactId>
<version>1.0.0.4</version>
</dependency>
2.在yml文件中增加个推相关配置
# 个推
getui:
baseUrl: https://restapi.getui.com/v2/
# 配置管理、应用配置中获取
appId: ***********************
appKey: **********************
appSecret: *******************
masterSecret: ****************
3.个推配置类
public class GeTuiConfig {
@Value("${getui.baseUrl}")
private String baseUrl;
@Value("${getui.appId}")
private String appId;
@Value("${getui.appKey}")
private String appKey;
@Value("${getui.appSecret}")
private String appSecret;
@Value("${getui.masterSecret}")
private String masterSecret;
@Bean(name = "myPushApi")
public PushApi pushApi() {
GtApiConfiguration apiConfiguration = new GtApiConfiguration();
//填写应用配置
apiConfiguration.setAppId(appId);
apiConfiguration.setAppKey(appKey);
apiConfiguration.setMasterSecret(masterSecret);
// 接口调用前缀,请查看文档: 接口调用规范 -> 接口前缀, 可不填写appId
apiConfiguration.setDomain(baseUrl);
// 实例化ApiHelper对象,用于创建接口对象
ApiHelper apiHelper = ApiHelper.build(apiConfiguration);
// 创建对象,建议复用。目前有PushApi、StatisticApi、UserApi
PushApi pushApi = apiHelper.creatApi(PushApi.class);
return pushApi;
}
}
4.个推工具类
public class PushUtils {
@Autowired
PushApi pushApi;
UserApi userApi;
/**
* 查询用户状态
*
* @param cid 客户端标识
*/
public void queryUserStatus(String cid) {
System.out.println(userApi.queryUserStatus(Sets.newHashSet(cid)));
}
/**
* 根据CID单推
*
* @throws InterruptedException
*/
public AjaxResult pushToSingleByCid(Msg msg) {
PushDTO<Audience> pushDTO = pushDTO(msg);
// 设置接收人信息
Audience audience = new Audience();
// 客户端标记
audience.addCid(msg.getCid());
pushDTO.setAudience(audience);
pushDTO.setRequestId(System.currentTimeMillis() + "");
ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
if (apiResult.isSuccess()) {
return AjaxResult.success(apiResult.getMsg(), apiResult.getData());
} else {
return AjaxResult.warn(apiResult.getMsg(), apiResult.getData());
}
}
/**
* 个推CID
*
* @return
*/
private PushDTO<Audience> pushDTO(Msg msg) {
// 根据CID进行单推
PushDTO<Audience> pushDTO = new PushDTO<Audience>();
// 设置推送参数
pushDTO.setRequestId(System.currentTimeMillis() + "");
pushDTO.setGroupName("g-name1");
Settings settings = new Settings();
// 消息离线时间设置,单位毫秒,-1表示不设离线,-1 ~ 3 * 24 * 3600 * 1000(3天)之间
settings.setTtl(3600000);
/**
* 【厂商通道策略】
*
* 默认所有通道的策略选择1-4
* 1: 表示该消息在用户在线时推送个推通道,用户离线时推送厂商通道;
* 2: 表示该消息只通过厂商通道策略下发,不考虑用户是否在线;
* 3: 表示该消息只通过个推通道下发,不考虑用户是否在线;
* 4: 表示该消息优先从厂商通道下发,若消息内容在厂商通道代发失败后会从个推通道下发。
* */
Strategy strategy = new Strategy();
strategy.setSt(2);
settings.setStrategy(strategy);
pushDTO.setSettings(settings);
/** 设置个推通道参数 */
PushMessage pushMessage = new PushMessage();
// 厂商消息内容
pushMessage.setTransmission(" {title:\"" + msg.getTitle() + "\",content:\"" + msg.getContent() + "\",uid:\"" + msg.getUid() + "\",read:false}");
pushDTO.setPushMessage(pushMessage);
// 厂商通道消息内容
PushChannel pushChannel = new PushChannel();
// 配置安卓产商参数
AndroidDTO androidDTO = new AndroidDTO();
Ups ups = new Ups();
ThirdNotification thirdNotification = new ThirdNotification();
thirdNotification.setClickType(CommonEnum.ClickTypeEnum.TYPE_STARTAPP.type);
// 离线消息【标题】
thirdNotification.setTitle(msg.getTitle());
// 离线消息【正文】
thirdNotification.setBody(msg.getContent());
ups.setNotification(thirdNotification);
// 设置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");
// 设置options 方式二
Map<String, Map<String, Object>> options = new HashMap<String, Map<String, Object>>();
Map<String, Object> all = new HashMap<String, Object>();
all.put("channel", "default");
options.put("ALL", all);
Map<String, Object> hw = new HashMap<String, Object>();
all.put("badgeAddNum", 3);
all.put("badgeClass", "com.getui.demo.GetuiSdkDemoActivity");
options.put("HW", hw);
ups.setOptions(options);
androidDTO.setUps(ups);
pushChannel.setAndroid(androidDTO);
IosDTO iosDTO = new IosDTO();
Aps aps = new Aps();
Alert alert = new Alert();
alert.setTitle("title-" + System.currentTimeMillis());
alert.setBody("ios_body");
aps.setAlert(alert);
iosDTO.setAps(aps);
pushChannel.setIos(iosDTO);
pushDTO.setPushChannel(pushChannel);
return pushDTO;
}
}
5.测试类
@Api("消息推送")
@RestController
@RequestMapping("/push/msg")
public class PushController extends BaseController {
@Autowired
private PushUtils pushUtils;
/**
* 根据CID单推
*
* @param msg
* @return
*/
@ApiOperation("消息单推")
@Log(title = "消息单推", businessType = BusinessType.INSERT)
@PostMapping("/single_push")
public AjaxResult pushToSingleByCid(Msg msg) {
return pushUtils.pushToSingleByCid(msg);
}
}