zoukankan      html  css  js  c++  java
  • 极光推送(转)

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

    转自http://blog.csdn.net/jidetashuo/article/details/54408657

  • 相关阅读:
    第九周周记
    第七周周记
    第三次作业第一题
    第五周周记
    《世界是数字的》读后感想
    第十周周记
    迷茫
    测试作业
    价值观作业
    作业二 感想
  • 原文地址:https://www.cnblogs.com/fengyifengceaser/p/7194885.html
Copyright © 2011-2022 走看看