上文說了個推的安卓和ios基礎配置。接下來說說他們的主要代碼實現。由於在具體使用中,我們一般會根據需要給不同的用戶推送信息,所以需要根據手機型號以及clientid鎖定手機。
一、獲取手機標識
首先,每個用戶下載了軟件後,我們獲取每部手機的clientid和型號並且傳到後臺,並且存到數據庫中。
$.plusReady(function() {
//獲取手機clientid
var clientid=plus.push.getClientInfo().clientid;
//獲取手機型號
var os=plus.os.name;
})
二、後臺代碼實現推送
// STEP1:獲取應用基本信息
private static String appId = "mrpcrCj4Gp7Q9yz3S6Xrx1";
private static String appKey = "vUocRU9R1b57hFDJcPpfG4";
private static String masterSecret = "4tuSYu9KXc74zEDymEgJ65";
private static String url = "http://sdk.open.api.igexin.com/apiex.htm";
/**
* 單個用戶Android推送(通知推送)
*
* @param user接受消息的人員
* @param title通知欄中的通知標題
* @param content通知欄的通知內容
*/
@RequestMapping("sendMesToAndroid")
@ResponseBody
// 向個推服務器發送請求
public static void sendMesToAndroid(User user,
String title, String content) throws IOException {
// http://localhost:8080/hydro/SendToPhone/sendMesToOne?title=sdsd&content=fggdgfd
String pushText = "{title:'" + title + "',content:'" + content
+ "',payload:'通知去幹嘛這裏可以自定義'}";
String clientid = user.getClientid();
IGtPush push = new IGtPush(appKey, masterSecret);
push.connect();
ListMessage message = new ListMessage();
message.setOffline(true);
// 離線有效時間,單位爲毫秒,可選
message.setOfflineExpireTime(24 * 3600 * 1000);
// 推送內容,格式爲{title:'通知標題',content:'通知內容',payload:'通知去幹嘛這裏可以自定義'}
TransmissionTemplate template = new TransmissionTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
template.setTransmissionContent(pushText);
template.setTransmissionType(2);
APNPayload payload = new APNPayload();
// payload.setBadge(0);
payload.setContentAvailable(1);
payload.setSound("default");
payload.setCategory("$由客戶端定義");
// 簡單模式APNPayload.SimpleMsg
payload.setAlertMsg(new APNPayload.SimpleAlertMsg(content));
template.setAPNInfo(payload);
message.setData(template);
// 可選,1爲wifi,0爲不限制網絡環境。根據手機處於的網絡情況,決定是否下發
message.setPushNetWorkType(0);
List<Target> targets = new ArrayList<Target>();
Target target = new Target();
target.setAppId(appId);
target.setClientId(clientid);
targets.add(target);
// 推送前通過該接口申請“ContentID”
String contentId = push.getContentId(message);
IPushResult ret = push.pushMessageToList(contentId, targets);
System.out.println(ret.getResponse().toString());
}
/**
* 單個用戶ios推送(透傳&APNS)
*
* @param user接受消息的人員
* @param title通知欄中的通知標題
* @param content通知欄的通知內容
* @return
*/
@RequestMapping("sendMesToIos")
@ResponseBody
public static String sendMesToIos( User user,
String title, String content) {
String clientid = user.getClientid();
String pushText = title + ":" + content;
IGtPush push = new IGtPush(url, appKey, masterSecret);
SingleMessage message = new SingleMessage();
TransmissionTemplate template = new TransmissionTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
template.setTransmissionContent(pushText);
// 透傳消息設置,1爲強制啓動應用,客戶端接收到消息後就會立即啓動應用;2爲等待應用啓動
template.setTransmissionType(2);
APNPayload payload = new APNPayload();
// 在已有數字基礎上加1顯示,設置爲-1時,在已有數字上減1顯示,設置爲數字時,顯示指定數字
// payload.setAutoBadge("+1");
payload.setAutoBadge("0");
payload.setContentAvailable(1);
// ios 12.0 以上可以使用 Dictionary 類型的 sound
payload.setSound("default");
payload.setCategory("$由客戶端定義");
// 簡單模式APNPayload.SimpleMsg
// payload.setAlertMsg(new
// APNPayload.SimpleAlertMsg(""+map.get("content")));
// 字典模式使用下者
HashMap<String, Object> map = new HashMap<>();
map.put("title", title);
map.put("cid", clientid);
map.put("payload", pushText);
map.put("body", content);
payload.setAlertMsg(getDictionaryAlertMsg(map));
template.setAPNInfo(payload);
message.setData(template);
message.setOffline(true);
// 離線有效時間,單位爲毫秒,可選
message.setOfflineExpireTime(24 * 1000 * 3600);
// 可選,1爲wifi,0爲不限制網絡環境。根據手機處於的網絡情況,決定是否下發
message.setPushNetWorkType(0);
Target target = new Target();
target.setAppId(appId);
target.setClientId(clientid);
IPushResult ret = null;
try {
ret = push.pushMessageToSingle(message, target);
} catch (RequestException e) {
e.printStackTrace();
ret = push.pushMessageToSingle(message, target, e.getRequestId());
}
if (ret != null) {
return ret.getResponse().toString();
} else {
return "";
}
}
}
三、前臺代碼實現推送
直接在登錄頁進行代碼處理(注:透傳,在創建通知欄消息的時候,如果用hbuild進行打包,則創建通知欄消息會創建一條就替換一條,也就是不能存在多個通知欄消息。百度說要用高版本的hbuildx進行打包就可以解決這個問題,親測有效)
$.plusReady(function() {
//監聽系統通知欄消息點擊事件
plus.push.addEventListener('click', function(msg){
// alert("點擊事件");
//處理點擊消息的業務邏輯代碼
}, false);
//監聽接收透傳消息事件
plus.push.addEventListener('receive', function(msg){
if(msg.type=='receive'){
//創建窗口消息
plus.push.createMessage(msg.content,null,null);
}
//處理透傳消息的業務邏輯代碼
}, false);
}
四、平臺測試指定手機用戶
- 測試獲取的手機cid是否有效,是否在線等,如果在線,會顯示該手機被分配到的deviceToken(測試ios證書用)
- 可以進行單個用戶測試
如果上一步驟顯示用戶在線,則可以進行單個用戶測試
- ios證書測試
如果需要ios的apns通道,需要配置證書,以下是證書的測試方式。(deviceToken的獲取看前文)
以上就是本人的個推推送使用經驗。如果有什麼不足,可以提出來一起交流。
- 可以進行單個用戶測試