JPush

项目很多模块都要使用到推送, 由于客户端面有android和ios两种。 所以选择了第三方组件极光推送。 

从jpush官网下载相应的包和sdk , 需要将sdk集成到app中,

服务器端只需要调用JPushClient类相应的方法就可以实现。

  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.  
  40.      */  
  41.     public static void sendPushNotice(String alias, String[] tags, String title, String btype, String content) {  
  42.         jpushClient = new JPushClient(MASTERSECRET, APPKEY, Integer.valueOf(DAY));  
  43.         PushPayload payload = null;  
  44.         // 生成推送的内容,这里我们先测试全部推送  
  45.         // 通知提示信息  
  46.         if (content != null) {  
  47.             Map<String, String> map = new HashMap<String, String>();  
  48.             map.put("btype", btype);  
  49.             // 根据别名推送  
  50.             if (alias != null && tags == null) {  
  51.                 payload = buldPushObject_all_all_alias(alias, title, content, map);  
  52.             } else if (alias == null && tags != null) { // 根据tag[]推送  
  53.                 payload = buldPushObject_all_all_tag(tags, title, content, map);  
  54.             } else if (alias != null && tags != null) { // 别名和tags[] 推送通知  
  55.                 payload = buldPushObject_all_all_aliasAndTag(alias, tags, title, content, map);  
  56.             } else if (alias == null && tags == null) {  
  57.                 payload = buldPushObject_all_all(title, content, map);  
  58.             }  
  59.         } else {  
  60.           
  61.         }  
  62.         try {  
  63.             System.out.println(payload.toString());  
  64.             PushResult result = jpushClient.sendPush(payload);  

  65.         } catch (APIConnectionException e) {  
  66.        
  67.         } catch (APIRequestException e) {  
  68.  
  69.         }  
  70.     }  
  71.   
  72.     /** 
  73.      * 推送自定义消息接口.根据别名修改标签(tag) 
  74.      * @param alias 别名 
  75.      * @param content 推送内容 
  76.      */  
  77.     public static void sendPushMessage(String alias, String content) {  
  78.         jpushClient = new JPushClient(MASTERSECRET, APPKEY, Integer.valueOf(DAY));  
  79.         PushPayload payload = null;  
  80.         // For push, all you need do is to build PushPayload object.  
  81.         // PushPayload payload = buildPushObject_all_all_alert();  
  82.         // 判断用户别名和tag不为空的情况下才推送修改标签(tag)  
  83.         if (content != null && alias != null) {  
  84.             payload = PushPayload.newBuilder()  
  85.                     .setAudience(Audience.alias(alias))  
  86.                     .setPlatform(Platform.all())  
  87.                     .setMessage(Message.content(content)).build();  
  88.         } else {  
  89.           
  90.         }  
  91.         try {  
  92.             System.out.println(payload.toString());  
  93.             PushResult result = jpushClient.sendPush(payload);  
  94.         } catch (APIConnectionException e) {  
  95.         
  96.         } catch (APIRequestException e) {  
  97.         
  98.         }  
  99.     }  
  100.   
  101.     /** 
  102.      * 查询记录推送成功条数 
  103.      * @param mid 
  104.      */  
  105.     public static void countPush(String mid) {  
  106.         jpushClient = new JPushClient(MASTERSECRET, APPKEY, Integer.valueOf(DAY));  
  107.         PushPayload payload = null;  
  108.         try {  
  109.             ReceivedsResult result = jpushClient.getReportReceiveds(mid);  
  110.             Received received = result.received_list.get(0);  
  111.         } catch (APIConnectionException e) {  
  112.   
  113.         } catch (APIRequestException e) {  

  114.         }  
  115.     }  
  116.   
  117.     /** 
  118.      * 统计用户数据。需要vip用户才能访问 
  119.      */  
  120.     public static void getReportUser() {  
  121.         jpushClient = new JPushClient(MASTERSECRET, APPKEY, Integer.valueOf(DAY));  
  122.         PushPayload payload = null;  
  123.         try {  
  124.             UsersResult result = jpushClient.getReportUsers(TimeUnit.DAY, "2015-04-28"8);  
  125.             log.debug("Got result - " + result);  
  126.         } catch (APIConnectionException e) {  
  127.         } catch (APIRequestException e) {  
  128.         }  
  129.     }  
  130.   
  131.     /** 
  132.      * 根据别名通知推送 
  133.      * @param alias 别名 
  134.      * @param alert 推送内容 
  135.      * @return 
  136.      */  
  137.     public static PushPayload buldPushObject_all_all_alias(String alias, String title, String content, Map<String, String> map) {  
  138.         return PushPayload  
  139.                 .newBuilder()  
  140.                 .setPlatform(Platform.all())  
  141.                 .setAudience(Audience.alias(alias))  
  142.                 .setNotification(  
  143.                         Notification  
  144.                                 .newBuilder()  
  145.                                 .addPlatformNotification(  
  146.                                         IosNotification.newBuilder()  
  147.                                                 .setAlert(content)  
  148.                                                 .addExtras(map).build())  
  149.                                 .addPlatformNotification(  
  150.                                         AndroidNotification.newBuilder()  
  151.                                                 .setAlert(content)  
  152.                                                 .setTitle(title).addExtras(map)  
  153.                                                 .build())  
  154.                                 .addPlatformNotification(  
  155.                                         WinphoneNotification.newBuilder()  
  156.                                                 .setAlert(content)  
  157.                                                 .addExtras(map).build())  
  158.                                 .build()).build();  
  159.     }  
  160.   
  161.     /** 
  162.      * 根据tag通知推送 
  163.      * @param alias 别名 
  164.      * @param alert 推送内容 
  165.      * @return 
  166.      */  
  167.     public static PushPayload buldPushObject_all_all_tag(String[] tags, String title, String content, Map<String, String> map) {  
  168.         return PushPayload  
  169.                 .newBuilder()  
  170.                 .setPlatform(Platform.all())  
  171.                 .setAudience(Audience.tag(tags))  
  172.                 .setNotification(  
  173.                         Notification  
  174.                                 .newBuilder()  
  175.                                 .addPlatformNotification(  
  176.                                         IosNotification.newBuilder()  
  177.                                                 .setAlert(content)  
  178.                                                 .addExtras(map).build())  
  179.                                 .addPlatformNotification(  
  180.                                         AndroidNotification.newBuilder()  
  181.                                                 .setAlert(content)  
  182.                                                 .setTitle(title).addExtras(map)  
  183.                                                 .build())  
  184.                                 .addPlatformNotification(  
  185.                                         WinphoneNotification.newBuilder()  
  186.                                                 .setAlert(content)  
  187.                                                 .addExtras(map).build())  
  188.                                 .build()).build();  
  189.     }  
  190.   
  191.     /** 
  192.      * 根据tag通知推送  
  193.      * @param alias  别名 
  194.      * @param alert  推送内容 
  195.      * @return 
  196.      */  
  197.     public static PushPayload buldPushObject_all_all_aliasAndTag(String alias, String[] tags, String title, String content, Map<String, String> map) {  
  198.         return PushPayload  
  199.                 .newBuilder()  
  200.                 .setPlatform(Platform.all())  
  201.                 .setAudience(Audience.alias(alias))  
  202.                 .setAudience(Audience.tag(tags))  
  203.                 .setNotification(  
  204.                         Notification  
  205.                                 .newBuilder()  
  206.                                 .addPlatformNotification(  
  207.                                         IosNotification.newBuilder()  
  208.                                                 .setAlert(content)  
  209.                                                 .addExtras(map).build())  
  210.                                 .addPlatformNotification(  
  211.                                         AndroidNotification.newBuilder()  
  212.                                                 .setAlert(content)  
  213.                                                 .setTitle(title).addExtras(map)  
  214.                                                 .build())  
  215.                                 .addPlatformNotification(  
  216.                                         WinphoneNotification.newBuilder()  
  217.                                                 .setAlert(content)  
  218.                                                 .addExtras(map).build())  
  219.                                 .build()).build();  
  220.     }  
  221.   
  222.     /** 
  223.      * 根据通知推送 
  224.      * @param alias 别名 
  225.      * @param alert 推送内容 
  226.      * @return 
  227.      */  
  228.     public static PushPayload buldPushObject_all_all(String title, String content, Map<String, String> map) {  
  229.         return PushPayload  
  230.                 .newBuilder()  
  231.                 .setPlatform(Platform.all())  
  232.                 .setAudience(Audience.all())  
  233.                 .setNotification(  
  234.                         Notification  
  235.                                 .newBuilder()  
  236.                                 .addPlatformNotification(  
  237.                                         IosNotification.newBuilder()  
  238.                                                 .setAlert(content)  
  239.                                                 .addExtras(map).build())  
  240.                                 .addPlatformNotification(  
  241.                                         AndroidNotification.newBuilder()  
  242.                                                 .setAlert(content)  
  243.                                                 .setTitle(title).addExtras(map)  
  244.                                                 .build())  
  245.                                 .addPlatformNotification(  
  246.                                         WinphoneNotification.newBuilder()  
  247.                                                 .setAlert(content)  
  248.                                                 .addExtras(map).build())  
  249.                                 .build()).build();  
  250.     }  
  251.   
  252. }  
注意苹果需要设置开发环境与生产环境 .默认为开发环境 .
PushPayload payload = PushPayload.newBuilder().setAudience(audienceList)
        .setNotification(notification).setOptions(Options.newBuilder().setApnsProduction(true).build())
        .setPlatform(Platform.all()).build();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值