由于工作需求,需要用到个推功能;在此纪录一下服务端(java)的使用
推送原理:
1:每部手机有对应的唯一cid:在手机对应应用程序(代码内)获取,获取方法如下
String cid = PushManager.getInstance().getClientid(NoticeListActivity.this);
2:安卓程序获取到该cid后需要传给服务端进行存储处理。
3:服务端进行推送时会根据每部手机的cid推送到对应的手机上
// 设置接收人信息
Audience audience = new Audience();
pushDTO.setAudience(audience);
audience.addCid(workOrder.getCid());
推送:展示在手机通知栏:有离线在线处理
透传:用来打开应用后进行的数据处理,可通过接受的数据进行页面跳转等
另外切记开通每个手机类型厂家的支持。
另外切记开通每个手机类型厂家的支持。
另外切记开通每个手机类型厂家的支持。
服务端使用:
1:引入个推依赖
2:配置个推的基本参数:注册个推账号后从账号里获取该信息(如图三)
3:调用个推进行消息推送:(代码较乱就不整理了)
public void pushUniApp(数据对象, GtApiConfiguration apiConfiguration) {
String address = ;
try {
ApiHelper apiHelper = ApiHelper.build(apiConfiguration);
PushApi pushApi = apiHelper.creatApi(PushApi.class);
PushDTO<Audience> pushDTO = new PushDTO<Audience>();
pushDTO.setRequestId(System.currentTimeMillis() + "");//requestid需要每次变化唯一
Strategy strategy = new Strategy();
strategy.setDef(1);
Settings settings = new Settings();
settings.setStrategy(strategy);
pushDTO.setSettings(settings);
settings.setTtl(3600000);//消息有效期,走厂商消息需要设置该值
//推送苹果离线通知标题内容
Alert alert = new Alert();
alert.setTitle("苹果离线通知栏标题");
alert.setBody("苹果离线通知栏内容");
Aps aps = new Aps();
aps.setContentAvailable(0);
aps.setSound("default");
aps.setAlert(alert);
IosDTO iosDTO = new IosDTO();
iosDTO.setAps(aps);
iosDTO.setType("notify");
PushChannel pushChannel = new PushChannel();
pushChannel.setIos(iosDTO);
//安卓离线厂商通道推送消息体
AndroidDTO androidDTO = new AndroidDTO();
Ups ups = new Ups();
ThirdNotification notification1 = new ThirdNotification();
;
ups.setNotification(notification1);
notification1.setTitle("测试标题");
notification1.setBody("测试内容:" + address + "");
notification1.setClickType("intent");
notification1.setIntent("intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=uni.UNIEC55EBA/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=${推送标题};S.content=${推送内容};S.payload=${payload};end");
androidDTO.setUps(ups);
pushChannel.setAndroid(androidDTO);
pushDTO.setPushChannel(pushChannel);
// PushMessage在线走个推通道才会起作用的消息体
PushMessage pushMessage = new PushMessage();
pushDTO.setPushMessage(pushMessage);
JSONObject jsonObject = new JSONObject();
jsonObject.put("title", "测试标题");
jsonObject.put("content", "测试内容:" + address + "");
JSONObject payload = new JSONObject();
//传递的数据处理
payload.put("borId", workOrder.getBwoId());
payload.put("boId", workOrder.getBoId());
jsonObject.put("payload", payload);
pushMessage.setTransmission(jsonObject.toJSONString());
// 设置接收人信息
Audience audience = new Audience();
pushDTO.setAudience(audience);
audience.addCid(workOrder.getCid());
// 进行cid单推
ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
if (apiResult.isSuccess()) {
// success
System.out.println("成功");
} else {
// failed
System.out.println("失败");
System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
}
IGtPush push = new IGtPush(UniUrl, AppKey, masterSecret);
IBatch batch = push.getBatch();
touClientNotifyMsg(workOrder, batch);
batch.submit();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 穿透模板
*
* @param workOrder
* @param batch
* @throws Exception
*/
private static void touClientNotifyMsg(对象类 workOrder, IBatch batch) throws Exception {
SingleMessage message = new SingleMessage();
ITemplate template = touTemplateDemo(workOrder);
message.setData(template);
message.setOffline(true);
message.setOfflineExpireTime(360 * 1000);
// 厂商通道下发策略
message.setStrategyJson("{\"default\":1,\"ios\":4,\"st\":1}");
Target target = new Target();
target.setAppId(UniAppUtil.AppId);
target.setClientId(workOrder.getCid());
batch.add(message, target);
}
public static TransmissionTemplate touTemplateDemo(BaseWorkOrder workOrder) {
TransmissionTemplate template = new TransmissionTemplate();
template.setAppId(UniAppUtil.AppId);
template.setAppkey(UniAppUtil.AppKey);
template.setTransmissionType(2);
Map map = new HashMap();
map.put("borId", workOrder.getBwoId());
map.put("boId", workOrder.getBoId());
template.setTransmissionContent(map.toString());
return template;
}
到此推送就结束了。距离时间太久,记忆比较模糊,有啥不准确的欢迎大家互相交流讨论