JPush极光推送个人理解

来源:互联网 发布:mysql 分类统计 编辑:程序博客网 时间:2024/05/17 01:26
[java] view plaincopy
  1. 个人代码例子  
  2. package com.lchy.xwx.mq.common.Jdpush;  
  3.   
  4. import java.util.HashMap;  
  5. import java.util.Map;  
  6. import org.slf4j.Logger;  
  7. import org.slf4j.LoggerFactory;  
  8. import com.lchy.xwx.mq.util.ReadConfigUtil;  
  9. import cn.jpush.api.JPushClient;  
  10. import cn.jpush.api.common.TimeUnit;  
  11. import cn.jpush.api.common.resp.APIConnectionException;  
  12. import cn.jpush.api.common.resp.APIRequestException;  
  13. import cn.jpush.api.push.PushResult;  
  14. import cn.jpush.api.push.model.Message;  
  15. import cn.jpush.api.push.model.Platform;  
  16. import cn.jpush.api.push.model.PushPayload;  
  17. import cn.jpush.api.push.model.audience.Audience;  
  18. import cn.jpush.api.push.model.notification.AndroidNotification;  
  19. import cn.jpush.api.push.model.notification.IosNotification;  
  20. import cn.jpush.api.push.model.notification.Notification;  
  21. import cn.jpush.api.push.model.notification.WinphoneNotification;  
  22. import cn.jpush.api.report.ReceivedsResult;  
  23. import cn.jpush.api.report.ReceivedsResult.Received;  
  24. import cn.jpush.api.report.UsersResult;  
  25.   
  26. public class Jdpush {  
  27.     protected static final Logger log = LoggerFactory.getLogger(Jdpush.class);  
  28.   
  29.     // demo App defined in resources/jpush-api.conf  
  30.     private static final ReadConfigUtil config = ReadConfigUtil.getInstance();  
  31.     private static final String APPKEY = config.getValue("jpush.appkey");  
  32.     private static final String MASTERSECRET = config.getValue("jpush.mastersecret");  
  33.     private static final String DAY = config.getValue("jpush.offlineday");  
  34.     public static JPushClient jpushClient = null;  
  35.   
  36.     /** 
  37.      * 推送通知接口 
  38.      * @param alias 别名 
  39.      * @param tags tag数组 
  40.      * @param title 推送标题 
  41.      * @param btype 推送类型 
  42.      * @param content 推送内容 
  43.      */  
  44.     public static void sendPushNotice(String alias, String[] tags, String title, String btype, String content) {  
  45.         jpushClient = new JPushClient(MASTERSECRET, APPKEY, Integer.valueOf(DAY));  
  46.         PushPayload payload = null;  
  47.         // 生成推送的内容,这里我们先测试全部推送  
  48.         // 通知提示信息  
  49.         if (content != null) {  
  50.             Map<String, String> map = new HashMap<String, String>();  
  51.             map.put("btype", btype);  
  52.             // 根据别名推送  
  53.             if (alias != null && tags == null) {  
  54.                 payload = buldPushObject_all_all_alias(alias, title, content, map);  
  55.             } else if (alias == null && tags != null) { // 根据tag[]推送  
  56.                 payload = buldPushObject_all_all_tag(tags, title, content, map);  
  57.             } else if (alias != null && tags != null) { // 别名和tags[] 推送通知  
  58.                 payload = buldPushObject_all_all_aliasAndTag(alias, tags, title, content, map);  
  59.             } else if (alias == null && tags == null) {  
  60.                 payload = buldPushObject_all_all(title, content, map);  
  61.             }  
  62.         } else {  
  63.             log.info("No notification - " + content);  
  64.         }  
  65.         try {  
  66.             System.out.println(payload.toString());  
  67.             PushResult result = jpushClient.sendPush(payload);  
  68.             System.out.println(result.msg_id+ "................................");  
  69.             log.info("Got result - " + result);  
  70.         } catch (APIConnectionException e) {  
  71.             log.error("Connection error. Should retry later. ", e);  
  72.         } catch (APIRequestException e) {  
  73.             log.error("Error response from JPush server. Should review and fix it. ", e);  
  74.             log.info("HTTP Status: " + e.getStatus());  
  75.             log.info("Error Code: " + e.getErrorCode());  
  76.             log.info("Error Message: " + e.getErrorMessage());  
  77.             log.info("Msg ID: " + e.getMsgId());  
  78.         }  
  79.     }  
  80.   
  81.     /** 
  82.      * 推送自定义消息接口.根据别名修改标签(tag) 
  83.      * @param alias 别名 
  84.      * @param content 推送内容 
  85.      */  
  86.     public static void sendPushMessage(String alias, String content) {  
  87.         jpushClient = new JPushClient(MASTERSECRET, APPKEY, Integer.valueOf(DAY));  
  88.         PushPayload payload = null;  
  89.         // For push, all you need do is to build PushPayload object.  
  90.         // PushPayload payload = buildPushObject_all_all_alert();  
  91.         // 判断用户别名和tag不为空的情况下才推送修改标签(tag)  
  92.         if (content != null && alias != null) {  
  93.             payload = PushPayload.newBuilder()  
  94.                     .setAudience(Audience.alias(alias))  
  95.                     .setPlatform(Platform.all())  
  96.                     .setMessage(Message.content(content)).build();  
  97.         } else {  
  98.             log.info("No notification - " + content);  
  99.         }  
  100.         try {  
  101.             System.out.println(payload.toString());  
  102.             PushResult result = jpushClient.sendPush(payload);  
  103.             System.out.println(result + "................................");  
  104.             log.info("Got result - " + result);  
  105.         } catch (APIConnectionException e) {  
  106.             log.error("Connection error. Should retry later. ", e);  
  107.         } catch (APIRequestException e) {  
  108.             log.error("Error response from JPush server. Should review and fix it. ", e);  
  109.             log.info("HTTP Status: " + e.getStatus());  
  110.             log.info("Error Code: " + e.getErrorCode());  
  111.             log.info("Error Message: " + e.getErrorMessage());  
  112.             log.info("Msg ID: " + e.getMsgId());  
  113.         }  
  114.     }  
  115.   
  116.     /** 
  117.      * 查询记录推送成功条数 
  118.      * @param mid 
  119.      */  
  120.     public static void countPush(String mid) {  
  121.         jpushClient = new JPushClient(MASTERSECRET, APPKEY, Integer.valueOf(DAY));  
  122.         PushPayload payload = null;  
  123.         try {  
  124.             ReceivedsResult result = jpushClient.getReportReceiveds(mid);  
  125.             Received received = result.received_list.get(0);  
  126.             System.out.println("android_received:" + received.android_received  
  127.                     + "\nios:" + received.ios_apns_sent);  
  128.             log.debug("Got result - " + result);  
  129.         } catch (APIConnectionException e) {  
  130.             // Connection error, should retry later  
  131.             log.error("Connection error, should retry later", e);  
  132.         } catch (APIRequestException e) {  
  133.             // Should review the error, and fix the request  
  134.             log.error("Should review the error, and fix the request", e);  
  135.             log.info("HTTP Status: " + e.getStatus());  
  136.             log.info("Error Code: " + e.getErrorCode());  
  137.             log.info("Error Message: " + e.getErrorMessage());  
  138.         }  
  139.     }  
  140.   
  141.     /** 
  142.      * 统计用户数据。需要vip用户才能访问 
  143.      */  
  144.     public static void getReportUser() {  
  145.         jpushClient = new JPushClient(MASTERSECRET, APPKEY, Integer.valueOf(DAY));  
  146.         PushPayload payload = null;  
  147.         try {  
  148.             UsersResult result = jpushClient.getReportUsers(TimeUnit.DAY, "2015-04-28"8);  
  149.             // Received received =result  
  150.             // System.out.println("android_received:"+received.android_received+"\nios:"+received.ios_apns_sent);  
  151.             log.debug("Got result - " + result);  
  152.         } catch (APIConnectionException e) {  
  153.             // Connection error, should retry later  
  154.             log.error("Connection error, should retry later", e);  
  155.         } catch (APIRequestException e) {  
  156.             // Should review the error, and fix the request  
  157.             log.error("Should review the error, and fix the request", e);  
  158.             log.info("HTTP Status: " + e.getStatus());  
  159.             log.info("Error Code: " + e.getErrorCode());  
  160.             log.info("Error Message: " + e.getErrorMessage());  
  161.         }  
  162.     }  
  163.   
  164.     /** 
  165.      * 根据别名通知推送 
  166.      * @param alias 别名 
  167.      * @param alert 推送内容 
  168.      * @return 
  169.      */  
  170.     public static PushPayload buldPushObject_all_all_alias(String alias, String title, String content, Map<String, String> map) {  
  171.         return PushPayload  
  172.                 .newBuilder()  
  173.                 .setPlatform(Platform.all())  
  174.                 .setAudience(Audience.alias(alias))  
  175.                 .setNotification(  
  176.                         Notification  
  177.                                 .newBuilder()  
  178.                                 .addPlatformNotification(  
  179.                                         IosNotification.newBuilder()  
  180.                                                 .setAlert(content)  
  181.                                                 .addExtras(map).build())  
  182.                                 .addPlatformNotification(  
  183.                                         AndroidNotification.newBuilder()  
  184.                                                 .setAlert(content)  
  185.                                                 .setTitle(title).addExtras(map)  
  186.                                                 .build())  
  187.                                 .addPlatformNotification(  
  188.                                         WinphoneNotification.newBuilder()  
  189.                                                 .setAlert(content)  
  190.                                                 .addExtras(map).build())  
  191.                                 .build()).build();  
  192.     }  
  193.   
  194.     /** 
  195.      * 根据tag通知推送 
  196.      * @param alias 别名 
  197.      * @param alert 推送内容 
  198.      * @return 
  199.      */  
  200.     public static PushPayload buldPushObject_all_all_tag(String[] tags, String title, String content, Map<String, String> map) {  
  201.         return PushPayload  
  202.                 .newBuilder()  
  203.                 .setPlatform(Platform.all())  
  204.                 .setAudience(Audience.tag(tags))  
  205.                 .setNotification(  
  206.                         Notification  
  207.                                 .newBuilder()  
  208.                                 .addPlatformNotification(  
  209.                                         IosNotification.newBuilder()  
  210.                                                 .setAlert(content)  
  211.                                                 .addExtras(map).build())  
  212.                                 .addPlatformNotification(  
  213.                                         AndroidNotification.newBuilder()  
  214.                                                 .setAlert(content)  
  215.                                                 .setTitle(title).addExtras(map)  
  216.                                                 .build())  
  217.                                 .addPlatformNotification(  
  218.                                         WinphoneNotification.newBuilder()  
  219.                                                 .setAlert(content)  
  220.                                                 .addExtras(map).build())  
  221.                                 .build()).build();  
  222.     }  
  223.   
  224.     /** 
  225.      * 根据tag通知推送  
  226.      * @param alias  别名 
  227.      * @param alert  推送内容 
  228.      * @return 
  229.      */  
  230.     public static PushPayload buldPushObject_all_all_aliasAndTag(String alias, String[] tags, String title, String content, Map<String, String> map) {  
  231.         return PushPayload  
  232.                 .newBuilder()  
  233.                 .setPlatform(Platform.all())  
  234.                 .setAudience(Audience.alias(alias))  
  235.                 .setAudience(Audience.tag(tags))  
  236.                 .setNotification(  
  237.                         Notification  
  238.                                 .newBuilder()  
  239.                                 .addPlatformNotification(  
  240.                                         IosNotification.newBuilder()  
  241.                                                 .setAlert(content)  
  242.                                                 .addExtras(map).build())  
  243.                                 .addPlatformNotification(  
  244.                                         AndroidNotification.newBuilder()  
  245.                                                 .setAlert(content)  
  246.                                                 .setTitle(title).addExtras(map)  
  247.                                                 .build())  
  248.                                 .addPlatformNotification(  
  249.                                         WinphoneNotification.newBuilder()  
  250.                                                 .setAlert(content)  
  251.                                                 .addExtras(map).build())  
  252.                                 .build()).build();  
  253.     }  
  254.   
  255.     /** 
  256.      * 根据通知推送 
  257.      * @param alias 别名 
  258.      * @param alert 推送内容 
  259.      * @return 
  260.      */  
  261.     public static PushPayload buldPushObject_all_all(String title, String content, Map<String, String> map) {  
  262.         return PushPayload  
  263.                 .newBuilder()  
  264.                 .setPlatform(Platform.all())  
  265.                 .setAudience(Audience.all())  
  266.                 .setNotification(  
  267.                         Notification  
  268.                                 .newBuilder()  
  269.                                 .addPlatformNotification(  
  270.                                         IosNotification.newBuilder()  
  271.                                                 .setAlert(content)  
  272.                                                 .addExtras(map).build())  
  273.                                 .addPlatformNotification(  
  274.                                         AndroidNotification.newBuilder()  
  275.                                                 .setAlert(content)  
  276.                                                 .setTitle(title).addExtras(map)  
  277.                                                 .build())  
  278.                                 .addPlatformNotification(  
  279.                                         WinphoneNotification.newBuilder()  
  280.                                                 .setAlert(content)  
  281.                                                 .addExtras(map).build())  
  282.                                 .build()).build();  
  283.     }  
  284.   
  285. }  

MASTERSECRET、APPKEY //在Jpush申请应用时产生的

Integer.valueOf(DAY)  //离线天数

jpushClient = new JPushClient(MASTERSECRET, APPKEY, Integer.valueOf(DAY));  //创建jpush对象


PushPayload
                .newBuilder()
                .setPlatform(Platform.all())   //推送设备类型,比如android、ios、Winphone等设备
                .setAudience(Audience.alias(alias))  //设备的别名
                .setNotification(     //推送通知消息的几种设备中,需要提供标题(title),内容(content),以及附加字段(map)。
                        Notification
                                .newBuilder()
                                .addPlatformNotification(
                                        IosNotification.newBuilder()
                                                .setAlert(content)
                                                .addExtras(map).build())
                                .addPlatformNotification(
                                        AndroidNotification.newBuilder()
                                                .setAlert(content)
                                                .setTitle(title).addExtras(map)
                                                .build())
                                .addPlatformNotification(
                                        WinphoneNotification.newBuilder()
                                                .setAlert(content)
                                                .addExtras(map).build())
                                .build()).build();


极光api


0 0