极光推送java服务端开发使用

1、申请注册账号--自己去官网注册

2、获得appKey和masterSecret

3、代码

首先明确一下推送的两个概念   消息(透传)和通知

通知:发送后会在系统通知栏收到展现,同时响铃或振动提醒用户。 ( mqtt不知道能不能发送通知,还在研究中...)
消息:以透传的形式传递给客户端,无显示,发送后不会在系统通知栏展现,第三方应用后需要开发者写代码才能看到。


写了一个简单工具类备用

[java]  view plain  copy
 print ?
  1. /**
  2.  * @date 2017年1月13日 
  3.  */  
  4. package xhsoft.yxd.admin.controller;  
  5.   
  6. import java.util.ArrayList;  
  7. import java.util.HashMap;  
  8. import java.util.List;  
  9. import java.util.Map;  
  10.   
  11. import cn.jpush.api.JPushClient;  
  12. import cn.jpush.api.common.resp.APIConnectionException;  
  13. import cn.jpush.api.common.resp.APIRequestException;  
  14. import cn.jpush.api.push.PushResult;  
  15. import cn.jpush.api.push.model.Message;  
  16. import cn.jpush.api.push.model.Options;  
  17. import cn.jpush.api.push.model.Platform;  
  18. import cn.jpush.api.push.model.PushPayload;  
  19. import cn.jpush.api.push.model.audience.Audience;  
  20. import cn.jpush.api.push.model.notification.AndroidNotification;  
  21. import cn.jpush.api.push.model.notification.IosNotification;  
  22. import cn.jpush.api.push.model.notification.Notification;  
  23.   
  24. /** 
  25.  * @author WangMeng 
  26.  * @date 2017年1月13日 
  27.  */  
  28. public class PushUtil  
  29. {  
  30.     /** 
  31.      * 给所有平台的所有用户发通知 
  32.      */  
  33.     public static void sendAllsetNotification(String message)  
  34.     {  
  35.         JPushClient jpushClient = new JPushClient("b6f47f289ee6dae11a529fcd",  
  36.                 "21742e62e41164461c4af259");  
  37.         //JPushClient jpushClient = new JPushClient(masterSecret, appKey);//第一个参数是masterSecret 第二个是appKey  
  38.         Map<String, String> extras = new HashMap<String, String>();  
  39.         // 添加附加信息  
  40.         extras.put("extMessage""我是额外的通知");  
  41.         PushPayload payload = buildPushObject_all_alias_alert(message, extras);  
  42.         try  
  43.         {  
  44.             PushResult result = jpushClient.sendPush(payload);  
  45.             System.out.println(result);  
  46.         }  
  47.         catch (APIConnectionException e)  
  48.         {  
  49.             System.out.println(e);  
  50.         }  
  51.         catch (APIRequestException e)  
  52.         {  
  53.             System.out.println(e);  
  54.             System.out.println("Error response from JPush server. Should review and fix it. " + e);  
  55.             System.out.println("HTTP Status: " + e.getStatus());  
  56.             System.out.println("Error Code: " + e.getErrorCode());  
  57.             System.out.println("Error Message: " + e.getErrorMessage());  
  58.             System.out.println("Msg ID: " + e.getMsgId());  
  59.         }  
  60.     }  
  61.     /** 
  62.      * 给所有平台的所有用户发消息 
  63.      *  
  64.      * @param message 
  65.      * @author WangMeng 
  66.      * @date 2017年1月13日 
  67.      */  
  68.     public static void sendAllMessage(String message)  
  69.     {  
  70.         JPushClient jpushClient = new JPushClient("b6f47f289ee6dae11a529fcd",  
  71.                 "21742e62e41164461c4af259");  
  72.         Map<String, String> extras = new HashMap<String, String>();  
  73.         // 添加附加信息  
  74.         extras.put("extMessage""我是额外透传的消息");  
  75.         PushPayload payload = buildPushObject_all_alias_Message(message, extras);  
  76.         try  
  77.         {  
  78.             PushResult result = jpushClient.sendPush(payload);  
  79.             System.out.println(result);  
  80.         }  
  81.         catch (APIConnectionException e)  
  82.         {  
  83.             System.out.println(e);  
  84.         }  
  85.         catch (APIRequestException e)  
  86.         {  
  87.             System.out.println(e);  
  88.             System.out.println("Error response from JPush server. Should review and fix it. " + e);  
  89.             System.out.println("HTTP Status: " + e.getStatus());  
  90.             System.out.println("Error Code: " + e.getErrorCode());  
  91.             System.out.println("Error Message: " + e.getErrorMessage());  
  92.             System.out.println("Msg ID: " + e.getMsgId());  
  93.         }  
  94.     }  
  95.     /** 
  96.      * 发送通知 
  97.      *  
  98.      * @param message 
  99.      * @param extras 
  100.      * @return 
  101.      * @author WangMeng 
  102.      * @date 2017年1月13日 
  103.      */  
  104.     private static PushPayload buildPushObject_all_alias_alert(String message,  
  105.             Map<String, String> extras)  
  106.     {  
  107.         return PushPayload.newBuilder()  
  108.                 .setPlatform(Platform.all())  
  109.                 // 设置平台  
  110.                 .setAudience(Audience.all())  
  111.                 // 按什么发送 tag alia  
  112.                 .setNotification(  
  113.                         Notification  
  114.                                 .newBuilder()  
  115.                                 .setAlert(message)  
  116.                                 .addPlatformNotification(  
  117.                                         AndroidNotification.newBuilder().addExtras(extras).build())  
  118.                                 .addPlatformNotification(  
  119.                                         IosNotification.newBuilder().addExtras(extras).build())  
  120.                                 .build())  
  121.                 // 发送消息  
  122.                 .setOptions(Options.newBuilder().setApnsProduction(true).build()).build();  
  123.                 //设置ios平台环境  True 表示推送生产环境,False 表示要推送开发环境   默认是开发    
  124.     }  
  125.     /** 
  126.      * 发送透传消息 
  127.      *  
  128.      * @param message 
  129.      * @param extras 
  130.      * @return 
  131.      * @author WangMeng 
  132.      * @date 2017年1月13日 
  133.      */  
  134.     private static PushPayload buildPushObject_all_alias_Message(String message,  
  135.             Map<String, String> extras)  
  136.     {  
  137.         return PushPayload.newBuilder().setPlatform(Platform.all())  
  138.         // 设置平台  
  139.         .setAudience(Audience.all())  
  140.             // 按什么发送 tag alia  
  141.             .setMessage(Message.newBuilder().setMsgContent(message).addExtras(extras).build())  
  142.             // 发送通知  
  143.             .setOptions(Options.newBuilder().setApnsProduction(true).build()).build();  
  144.         //设置ios平台环境  True 表示推送生产环境,False 表示要推送开发环境   默认是开发    
  145.     }  
  146.   
  147.     /** 
  148.      * 客户端 给所有平台的一个或者一组用户发送信息 
  149.      */  
  150.     public static void sendAlias(String message, List<String> aliasList)  
  151.     {  
  152.         JPushClient jpushClient = new JPushClient("b6f47f289ee6dae11a529fcd",  
  153.                 "21742e62e41164461c4af259");  
  154.         Map<String, String> extras = new HashMap<String, String>();  
  155.         // 添加附加信息  
  156.         extras.put("extMessage""我是额外的消息--sendAlias");  
  157.   
  158.         PushPayload payload = allPlatformAndAlias(message, extras, aliasList);  
  159.         try  
  160.         {  
  161.             PushResult result = jpushClient.sendPush(payload);  
  162.             System.out.println(result);  
  163.         }  
  164.         catch (APIConnectionException e)  
  165.         {  
  166.             System.out.println(e);  
  167.         }  
  168.         catch (APIRequestException e)  
  169.         {  
  170.             System.out.println(e);  
  171.             System.out.println("Error response from JPush server. Should review and fix it. " + e);  
  172.             System.out.println("HTTP Status: " + e.getStatus());  
  173.             System.out.println("Error Code: " + e.getErrorCode());  
  174.             System.out.println("Error Message: " + e.getErrorMessage());  
  175.             System.out.println("Msg ID: " + e.getMsgId());  
  176.         }  
  177.     }  
  178.   
  179.     /** 
  180.      * 极光推送:生成向一个或者一组用户发送的消息。 
  181.      */  
  182.     private static PushPayload allPlatformAndAlias(String alert, Map<String, String> extras,  
  183.             List<String> aliasList)  
  184.     {  
  185.   
  186.         return PushPayload  
  187.                 .newBuilder()  
  188.                 .setPlatform(Platform.all())  
  189.                 .setAudience(Audience.alias(aliasList))  
  190.                 .setNotification(  
  191.                         Notification  
  192.                                 .newBuilder()  
  193.                                 .setAlert(alert)  
  194.                                 .addPlatformNotification(  
  195.                                         AndroidNotification.newBuilder().addExtras(extras).build())  
  196.                                 .addPlatformNotification(  
  197.                                         IosNotification.newBuilder().addExtras(extras).build())  
  198.                                 .build())  
  199.                 .setOptions(Options.newBuilder().setApnsProduction(true).build()).build();  
  200.     }  
  201.     /** 
  202.      * 客户端 给平台的一个或者一组标签发送消息。 
  203.      */  
  204.     public static void sendTag(String message, String messageId, String type, List<String> tagsList)  
  205.     {  
  206.         JPushClient jpushClient = new JPushClient("290fd77fa503ebcef8112857",  
  207.                 "e1fef546e12c29c72bf8feef");  
  208.         // 附加字段  
  209.         Map<String, String> extras = new HashMap<String, String>();  
  210.         extras.put("messageId", messageId);  
  211.         extras.put("typeId", type);  
  212.   
  213.         PushPayload payload = allPlatformAndTag(message, extras, tagsList);  
  214.         try  
  215.         {  
  216.             PushResult result = jpushClient.sendPush(payload);  
  217.             System.out.println(result);  
  218.         }  
  219.         catch (APIConnectionException e)  
  220.         {  
  221.             System.out.println(e);  
  222.         }  
  223.         catch (APIRequestException e)  
  224.         {  
  225.             System.out.println(e);  
  226.             System.out.println("Error response from JPush server. Should review and fix it. " + e);  
  227.             System.out.println("HTTP Status: " + e.getStatus());  
  228.             System.out.println("Error Code: " + e.getErrorCode());  
  229.             System.out.println("Error Message: " + e.getErrorMessage());  
  230.             System.out.println("Msg ID: " + e.getMsgId());  
  231.         }  
  232.     }  
  233.   
  234.     /** 
  235.      * 极光推送:生成向一组标签进行推送的消息。 
  236.      */  
  237.     private static PushPayload allPlatformAndTag(String alert, Map<String, String> extras,  
  238.             List<String> tagsList)  
  239.     {  
  240.   
  241.         return PushPayload  
  242.                 .newBuilder()  
  243.                 .setPlatform(Platform.android_ios())  
  244.                 .setAudience(Audience.tag(tagsList))  
  245.                 .setNotification(  
  246.                         Notification  
  247.                                 .newBuilder()  
  248.                                 .setAlert(alert)  
  249.                                 .addPlatformNotification(  
  250.                                         AndroidNotification.newBuilder().addExtras(extras).build())  
  251.                                 .addPlatformNotification(  
  252.                                         IosNotification.newBuilder().addExtras(extras).build())  
  253.                                 .build())  
  254.                 .setOptions(Options.newBuilder().setApnsProduction(true).build()).build();  
  255.     }  
  256.   
  257.     public static void main(String[] args)  
  258.     {  
  259.         // new PushUtil().sendAll("这是java后台发送的一个通知。。。。");  
  260. //      List<String> sendAlias = new ArrayList<>();  
  261. //      sendAlias.add("1001");  
  262. //      new PushUtil().sendAlias("这是java后台发送的一个按照alia的通知", sendAlias);  
  263.           
  264.         new PushUtil().sendAllMessage("这是后台发送的透传消息");  
  265.     }  
  266. }  
更加细节的部分参考 官网文档  https://docs.jiguang.cn/jpush/server/3rd/java_sdk/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值