首先進入友盟官網去建立添加一個應用,以獲取應用對應的Appkey和Umeng Message Secret。java
而後進入開發者中心下載集成示例:android
解壓後,能夠看到demo文件,以android廣播推送爲例,咱們須要:api
應用的appkey和appMasterSecret,DeviceToken 設備標識,Ticker 通知欄提示文字,Title 標題以及點擊後顯示的文字Text。app
public void sendAndroidBroadcast() throws Exception { AndroidBroadcast broadcast = new AndroidBroadcast(appkey,appMasterSecret); broadcast.setTicker( "Android broadcast ticker"); broadcast.setTitle( "中文的title"); broadcast.setText( "Android broadcast text"); broadcast.goAppAfterOpen(); broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION); // TODO Set 'production_mode' to 'false' if it's a test device. // For how to register a test device, please see the developer doc. broadcast.setProductionMode(); client.send(broadcast); }
自定義接收類UmengResultpost
@Getter @Setter public class UmengResult { private String ret; private JSONObject data; }
demo中,sendAndroidBroadcast()測試
public void sendAndroidBroadcast() throws Exception { AndroidBroadcast broadcast = new AndroidBroadcast(appkey,appMasterSecret); broadcast.setTicker( "Android broadcast ticker"); broadcast.setTitle( "中文的title"); broadcast.setText( "Android broadcast text"); broadcast.goAppAfterOpen();//默認點擊消息拉活app broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION); broadcast.setProductionMode();//生產模式,一開始改爲測試避免手滑哦 //能夠使用setCustomField()給客戶端推送附加數據 JSONObject extraData = new JSONObject(); extraData.put("img","imgUrl"); extraData.put("source",new source()); broadcast.setCustomField(extraData); //設置推送的有效期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); c1.add(Calendar.MINUTE, 1); broadcast.setStartTime(sdf.format(c1.getTime())); // 友盟推送的有效期延遲一天 c2.add(Calendar.DAY_OF_MONTH, 1); broadcast.setExpireTime(sdf.format(c2.getTime())); String result = client.send(broadcast); //獲取推送消息的任務id UmengResult umengResult = JSON.parseObject(status.toString(), UmengResult.class); String taskid = umengResult.getData().get("task_id").toString(); }
查閱api文檔,能夠看到獲取任務狀態的api爲 '/api/status',須要如下參數url
{ "appkey":"xx", // 必填, 應用惟一標識 "timestamp":"xx", // 必填, 時間戳,10位或者13位都可,時間戳有效期爲10分鐘 "task_id":"xx" // 必填, 消息發送時, 從返回消息中獲取的task_id }
打開pushClient.java,自定義咱們獲取狀態的方法,代碼以下:spa
protected static final String statusPath = "/api/status"; public String getPushStatus(String appkey,String task_id,String appMasterSecret) throws Exception { JSONObject getStatus = new JSONObject(); getStatus.put("appkey", appkey); String timestamp = Integer.toString((int)(System.currentTimeMillis() / 1000)); getStatus.put("timestamp", timestamp); getStatus.put("task_id",task_id); String url = host + statusPath; String postBody = getStatus.toString(); String sign = DigestUtils.md5Hex(("POST" + url + postBody + appMasterSecret).getBytes(StandardCharsets.UTF_8)); url = url + "?sign=" + sign; HttpPost post = new HttpPost(url); post.setHeader("User-Agent", USER_AGENT); StringEntity se = new StringEntity(postBody, "UTF-8"); post.setEntity(se); // Send the post request and get the response HttpResponse response = client.execute(post); System.out.println("Response Code : " + response.getStatusLine().getStatusCode()); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } return result.toString(); }
示例接口code
@RequestMapping(value = "/getStatus") public CommonResult GetStatus(@RequestBody MessageInfo model) throws Exception { PushClient pushClient = new PushClient(); String s = pushClient.getPushStatus(model.getAppId(), model.getUmengTaskId(), model.getSecret()); UmengResult umengResult = JSON.parseObject(s, UmengResult.class); //推送接收統計數目 model.setSendCount((Integer) umengResult.getData().get("sent_count")); //推送打開統計數目 model.setOpenCount((Integer) umengResult.getData().get("open_count")); //推送被忽略統計數目 model.setDismissCount((Integer) umengResult.getData().get("dismiss_count")); messageInfoService.update(model); return processSuccess("更新成功"); }
至此,U-Push的推送和獲取後續狀態的實例就完成了,單播,組播均可以用相似寫法。orm