個推官網(wǎng)上這些都有,只是兩者之間沒有聯(lián)系起來守谓,JAVA寫后臺代碼在我們iOS端是如何展示的穿铆。下面就具體講講他們的聯(lián)系把。
這是iOS集成代碼和講解具體作用:http://docs.getui.com/mobile/ios/xcode/#2
這是javad :http://docs.getui.com/server/java/start/
這里的代碼寫的很詳細斋荞,但要兩者都結(jié)合起來就比較麻煩了荞雏。
個推java代碼都是適用與安卓的,他們就是以安卓作為模板來進行編寫的,所以在iOS方面就要謹慎了凤优。
首先我們要了解iOS接受push的格式悦陋,我們只有發(fā)送這種格式iOS才能真確接收,可以產(chǎn)考https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html筑辨,
可視化請看圖
image.png
image.png
這就是我們APNS推送格式俺驶,如果不是這種格式那就可能導致無法推送到達。
知道了iOS的接受格式挖垛,那我們寫java代碼就有了一個方向了痒钝。
首先我們要找到j(luò)ava文檔的推送模板中的透傳消息模板,這個模板是適用與iOS的痢毒。
public String getAppPush()
{
IGtPush push = new IGtPush(host, appKey, masterSecret);
TransmissionTemplate template = getTemplate();
SingleMessage message = new SingleMessage();
message.setOffline(true);
// 離線有效時間送矩,單位為毫秒,可選
message.setOfflineExpireTime(24 * 3600 * 1000);
message.setData(template);
// 可選哪替,1為wifi栋荸,0為不限制網(wǎng)絡(luò)環(huán)境。根據(jù)手機處于的網(wǎng)絡(luò)情況凭舶,決定是否下發(fā)
message.setPushNetWorkType(0);
Target target = new Target();
target.setAppId(appId);
target.setClientId(CID1);
//target.setAlias(Alias);
IPushResult ret = null;
try {
ret = push.pushMessageToSingle(message, target);
} catch (RequestException e) {
e.printStackTrace();
ret = push.pushMessageToSingle(message, target, e.getRequestId());
}
if (ret != null) {
System.out.println(ret.getResponse().toString());
} else {
System.out.println("服務(wù)器響應(yīng)異常");
}
System.out.println(ret.getResponse().toString());
return "";
}
public static TransmissionTemplate getTemplate() {
TransmissionTemplate template = new TransmissionTemplate();
template.setAppId(appId);
template.setAppkey(appkey);
Map<String, Object> contentMap = new HashMap<String, Object>();
contentMap.put( "pushType", 11 );
contentMap.put( "content", "我是消息" );
String jsonString = JSON.toJSONString(contentMap);
// 透傳消息設(shè)置晌块,1為強制啟動應(yīng)用,客戶端接收到消息后就會立即啟動應(yīng)用帅霜;2為等待應(yīng)用啟動
template.setTransmissionType(2);
template.setTransmissionContent(jsonString);
APNPayload payload = new APNPayload();
//在已有數(shù)字基礎(chǔ)上加1顯示匆背,設(shè)置為-1時,在已有數(shù)字上減1顯示身冀,設(shè)置為數(shù)字時钝尸,顯示指定數(shù)字
payload.setAutoBadge("+1");
payload.setContentAvailable(1);
payload.setSound("default");
payload.setCategory("$由客戶端定義");
//簡單模式APNPayload.SimpleMsg
// payload.setAlertMsg(new APNPayload.SimpleAlertMsg("hello"));
//字典模式使用APNPayload.DictionaryAlertMsg
payload.setAlertMsg(getDictionaryAlertMsg());
// 添加多媒體資源
payload.addMultiMedia(new MultiMedia().setResType(MultiMedia.MediaType.video)
.setResUrl("http://ol5mrj259.bkt.clouddn.com/test2.mp4")
.setOnlyWifi(true));
template.setAPNInfo(payload);
return template;
}
private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(){
APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
alertMsg.setBody("body");
alertMsg.setActionLocKey("ActionLockey");
alertMsg.setLocKey("LocKey");
alertMsg.addLocArg("loc-args");
alertMsg.setLaunchImage("launch-image");
// iOS8.2以上版本支持
alertMsg.setTitle("Title");
alertMsg.setTitleLocKey("TitleLocKey");
alertMsg.addTitleLocArg("TitleLocArg");
return alertMsg;
}
這是我們推送的完整代碼了,控制我們推送上面顯示樣式就是在getDictionaryAlertMsg()中搂根。這里傳的什么數(shù)據(jù)類型要和前端商量好珍促,不然會出現(xiàn)錯誤(接受到推送數(shù)據(jù),打開app就可能會導致崩潰)剩愧。
這里我們都是以cid目標進行個推猪叙,沒有廣推,廣推這里就不做講述了仁卷。