当前位置 博文首页 > 知晓汝名,吓吾一跳!:友盟umeng消息推送直接复制就能用(纯干货

    知晓汝名,吓吾一跳!:友盟umeng消息推送直接复制就能用(纯干货

    作者:知晓汝名,吓吾一跳! 时间:2021-06-05 18:19

    一. 单播推送(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
    bk
    下一篇:没有了