一. 單播推送(unicast)
1.1 圖
1.2 程式碼
1 /**
2 * 根據裝置的deviceToken, 去給指定的裝置推送訊息
3 *
4 * @param deviceToken 單個deviceToken, 指定裝置
5 */
6 public void sendAndroidUnicast(String deviceToken) throws Exception {
7
8 AndroidUnicast unicast = new AndroidUnicast(appkey, appMasterSecret);
9
10 // Android端會獲取到 device_token, 直接找Android工程師要就行
11 unicast.setDeviceToken(deviceToken);
12
13 // 當你沒有下拉通知欄的時候, 寫入的文字會在頂端翻轉顯示
14 unicast.setTicker("看這裡");
15
16 // 標題
17 unicast.setTitle("單播推送");
18
19 // 內容
20 unicast.setText("今晚啤酒小燒烤");
21
22 // 修改以後, 使用過程中需要跟Android商量好
23 unicast.goAppAfterOpen(); // 點選"通知"的後續行為,預設為開啟app。
24 // unicast.goUrlAfterOpen("127.0.0.1"); // 點選"通知"的後續行為,跳轉到URL。
25 // unicast.goActivityAfterOpen(""); // 點選"通知"的後續行為,開啟特定的activity。
26 // unicast.goCustomAfterOpen(""); // 點選"通知"的後續行為,使用者自定義內容, 可傳輸JSONObject, 也可傳輸String。
27
28 /**
29 * NOTIFICATION 是友盟做處理在通知欄上顯示通知內容
30 * MESSAGE 是傳給應用自身進行解析處理
31 */
32 unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
33
34 // 測試模式使用false, 正式模式使用true
35 unicast.setProductionMode();
36
37 // 自定義的一些東西
38 unicast.setExtraField("test", "helloworld");
39 unicast.setChannelActivity("your channel activity");
40 unicast.setChannelProperties("abc");
41
42 client.send(unicast);
43 }
二. 列播推送(listcast)
由於列播推送, 友盟當中的SDK 並沒有給, 所以後面自己寫了一份
1.1 圖
2.2 程式碼(包括所用實體類)
2.2.1 實體類
1 import push.AndroidNotification;
2
3 /**
4 * 列播(要求不超過500個device_token) 用英文逗號隔開
5 */
6 public class AndroidColumnOn extends AndroidNotification {
7
8 public AndroidColumnOn(String appkey,String appMasterSecret) throws Exception {
9 setAppMasterSecret(appMasterSecret);
10 setPredefinedKeyValue("appkey", appkey);
11 this.setPredefinedKeyValue("type", "listcast"); // type = listcast 是群體傳送
12 }
13
14 public void setDeviceToken(String token) throws Exception {
15 setPredefinedKeyValue("device_tokens", token);
16 }
17
18 }
2.2.2 推送程式碼
1 /**
2 * 列播推送
3 *
4 * @param deviceToken 多個device_tokens是用英文逗號間隔, 不能超過五百個
5 */
6 public void sendAndroidColumnOn(String deviceToken) throws Exception {
7 // 自定義實體類, 附有程式碼
8 AndroidColumnOn columnOn = new AndroidColumnOn(appkey, appMasterSecret);
9
10 // 列播中 傳入的deviceToken類似於 1,2,3,4,5
11 columnOn.setDeviceToken(deviceToken);
12
13 // 當你沒有下拉通知欄的時候, 寫入的文字會在頂端翻轉顯示, 有的可以顯示有的不可以顯示, 看你設定和裝置的允許情況
14 columnOn.setTicker("看過來");
15
16 // 標題
17 columnOn.setTitle("群體推送");
18
19 // 內容
20 columnOn.setText("魚香肉絲");
21
22 // 點選"通知"的後續行為,預設為開啟app。
23 columnOn.goAppAfterOpen();
24
25 /**
26 * NOTIFICATION 是友盟做處理在通知欄上顯示通知內容
27 * MESSAGE 是傳給應用自身進行解析處理
28 */
29 columnOn.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
30
31 // 測試模式使用false, 正式模式使用true
32 columnOn.setProductionMode();
33
34 // Set customized fields
35 columnOn.setExtraField("test", "helloworld");
36
37 // 自定義的一些東西
38 columnOn.setChannelActivity("your channel activity");
39 columnOn.setChannelProperties("abc");
40 client.send(columnOn);
41 }
三. 廣播推送(boradcast)
廣播推送只有 setProductionMode 是ture , 也就是正式模式才可以使用,只會將訊息傳送給測試裝置
3.1 圖
3.2 程式碼
1 /**
2 * 廣播模式不需要device_tokens, 應該是根據你的 appkey 和 appMasterSecret 找到你所儲存在這兩裡面的device_tokens, 然後整個推送
3 */
4 public void sendAndroidBroadcast() throws Exception {
5 AndroidBroadcast broadcast = new AndroidBroadcast(appkey, appMasterSecret);
6 broadcast.setTicker("Android broadcast ticker");
7 broadcast.setTitle("這是廣播");
8 broadcast.setText("廣播資料");
9 broadcast.goAppAfterOpen();
10
11 broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
12 // 預設ture, 如果是false的話是收不到推送的
13 broadcast.setProductionMode();
14 // 自定義資訊
15 broadcast.setExtraField("test", "helloworld");
16 //廠商通道相關引數
17 broadcast.setChannelActivity("your channel activity");
18 broadcast.setChannelProperties("abc");
19 client.send(broadcast);
20 }
四. 廣播推送狀態(引數為task_id)
4.1 圖
4.2 程式碼
1 /**
2 * 廣播推送狀態 根據taskId 獲取推送成功以後數量
3 */
4 private void pushStatus(String taskId){
5 try {
6 String s = client.getPushStatus(appkey, taskId, appMasterSecret);
7 System.out.println(s);
8 UMengStatusResult result = JSON.parseObject(s, UMengStatusResult.class);
9 //推送接收統計數目
10 Map<String, Object> data = result.getData();
11 System.out.println("推送接收統計數目 -> " + data.get("sent_count"));
12 System.out.println("推送開啟統計數目 -> " + data.get("open_count"));
13 System.out.println("推送被忽略統計數目 -> " + data.get("dismiss_count"));
14 } catch (Exception e) {
15 e.printStackTrace();
16 }
17 }
1 public String getPushStatus(String appkey,String task_id,String appMasterSecret) throws Exception {
2 JSONObject getStatus = new JSONObject();
3
4 getStatus.put("appkey", appkey);
5
6 String timestamp = Integer.toString((int)(System.currentTimeMillis() / 1000));
7
8 getStatus.put("timestamp", timestamp);
9
10 getStatus.put("task_id",task_id);
11
12 String url = host + statusPath;
13
14 String postBody = getStatus.toString();
15
16 String sign = DigestUtils.md5Hex(("POST" + url + postBody + appMasterSecret).getBytes("utf8"));
17
18 url = url + "?sign=" + sign;
19
20 HttpPost post = new HttpPost(url);
21
22 post.setHeader("User-Agent", USER_AGENT);
23
24 StringEntity se = new StringEntity(postBody, "UTF-8");
25
26 post.setEntity(se);
27
28 // post請求獲取響應
29 HttpResponse response = client.execute(post);
30
31 System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
32
33 BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
34
35 StringBuffer result = new StringBuffer();
36 String line = "";
37 while ((line = rd.readLine()) != null) {
38 result.append(line);
39 }
40
41 return result.toString();
42 }
檔案播類似於列播, 區別是 列播用英文逗號分隔, 而檔案播用 \n 分隔(檔案播使用就是把所有device_tokens用 \n分隔, 然後存入檔案中, 傳送給友盟)
關於 alias 這些播送的, 可以試著自己寫一下, 總體辦法就是將 device_tokens 繫結設定的 alias , 然後把 傳送給友盟的 device_token變成了 alias傳送。
附加友盟開發者中心連結:
https://developer.umeng.com/docs/66632/detail/68343#h1-u6D88u606Fu53D1u90014
引數文件:
https://developer.umeng.com/docs/67966/detail/149296#h1--g-7