• 极光推送代码介绍


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

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

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

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

    PushPayload
                    .newBuilder()
                    .setPlatform(Platform.all())   //推送设备类型,比如AndroidiOS、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();

  • 相关阅读:
    asp.net判断访问者是否来自移动端
    Chrome模拟手机浏览器(iOS/Android)的三种方法,亲测无误!
    html有序列表和无序列表
    ASP.Net 类(CS)文件怎样获取Web应用程序的路径
    Web Service无法加载协定为“ServiceReference1.xxxxxx”的终结点配置部分,因为找到了该协定的多个终结点配置。请按名称指示首选的终结点配置部分
    Web serviser请求通道在等待 00:00:59.6479648 以后答复时超时。增加传递给请求调用的超时值,或者增加绑定上的 SendTimeout 值。分配给此操作的时间可能是更长超时的一部分。
    ASP.NET C# 文件下载
    PHP 取网页变量
    PHP 乘法口诀表
    Apache
  • 原文地址:https://www.cnblogs.com/lvgg/p/6698603.html
Copyright © 2020-2023  润新知