/**
* 推送透传消息给某个人
* @param clientId
* @param type 类型0 浇筑令 1 配合比
* @param text 类型里的文字
* @param transmissionContent 这是透传内容
* @param txtBody 这个是推送状态栏的头文字信息
* @param txtTitle 这个是推送状态栏的内容信息
* @return
*/
public static boolean pushSingleUser(String clientId,
String type, String text, String transmissionContent,
String txtBody,String txtTitle){
IGtPush push = new IGtPush(url, appKey, masterSecret);
TransmissionTemplate template =getTemplate(type,text,transmissionContent,txtBody,txtTitle);
// 定义"AppMessage"类型消息对象,设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
SingleMessage message = new SingleMessage();
message.setData(template);
// 设置消息离线,并设置离线时间
message.setOffline(true);
// 离线有效时间,单位为毫秒,可选
message.setOfflineExpireTime(24 * 1000 * 3600);
Target target = new Target();
//Target target2 = new Target();
target.setAppId(appId);
target.setClientId(clientId);
IPushResult ret =null;
try{
ret= push.pushMessageToSingle(message, target);
} catch (RequestException e) {
ret= push.pushMessageToSingle(message, target);
return false;
}catch (Exception e) {
return false;
}
if (ret != null) {
System.out.println(ret.getResponse().toString());
return true;
} else {
System.out.println("服务器响应异常");
return false;
}
}
/**
*
* @param type 类型0 1
* @param text 类型里的文字
* @param transmissionContent 这是透传内容
* @param txtBody 这个是推送状态栏的头文字信息
* @param txtTitle 这个是推送状态栏的内容信息
* @return
*/
public static TransmissionTemplate getTemplate(String type,
String text, String transmissionContent,String txtBody,String txtTitle) {
TransmissionTemplate template = new TransmissionTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
template.setTransmissionContent
("{\"type\":\""+type+"\", \"text\":\""+text+"\" }");
template.setTransmissionType(1);
APNPayload payload = new APNPayload();
//在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字
payload.setAutoBadge("+1");
payload.setContentAvailable(1);
//ios 12.0 以上可以使用 Dictionary 类型的 sound
payload.setSound("default");
payload.setCategory("$由客户端定义");
Map<String,String> map = new HashMap<String,String>();
map.put("type", type);
map.put("text", text);
payload.addCustomMsg("payload", map);
//字典模式使用APNPayload.DictionaryAlertMsg
payload.setAlertMsg(getDictionaryAlertMsg(txtBody,txtTitle));
//设置语音播报类型,int类型,0.不可用
payload.setVoicePlayType(0);
template.setAPNInfo(payload);
return template;
}
private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String txtBody,String txtTitle){
APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
alertMsg.setBody(txtBody);
alertMsg.setActionLocKey("ActionLockey");
alertMsg.setLocKey(txtBody);
alertMsg.addLocArg("loc-args");
alertMsg.setLaunchImage("launch-image");
// iOS8.2以上版本支持
alertMsg.setTitle(txtTitle);
alertMsg.setTitleLocKey("TitleLocKey");
alertMsg.addTitleLocArg("TitleLocArg");
return alertMsg;
}
public static void main(String[] args) throws Exception {
pushSingleUser(clientId,"0","df有一个未审核","透传内容","tou","您有一条未审核的**");
}
最初的想法是在APN通道发送json展示状态,但是问了技术说 online状态不能用APN,就是手机在app内的时候这个json是传不过去的,所以我和app端商量后,代码中是在("{\"type\":\""+type+"\", \"text\":\""+text+"\" }") 这里组织的状态。
推送给iOS应用的消息,如果是online状态,则是用个推通道推送的透传消息,如果是offline状态则是用APN通道推送的,会有通知栏消息提示的 |
然后我又了解到:api推送隔天展示记录,所以我在官网看不到刚推送的消息;