您的位置:首页 > 编程语言 > Java开发

Jpush 极光推送java服务器端

2017-08-02 16:04 483 查看
来源 : http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
  1 import cn.jpush.api.JPushClient;2 import cn.jpush.api.common.resp.APIConnectionException;3 import cn.jpush.api.common.resp.APIRequestException;4 import cn.jpush.api.push.PushResult;5 import cn.jpush.api.push.model.Message;6 import cn.jpush.api.push.model.Options;7 import cn.jpush.api.push.model.Platform;8 import cn.jpush.api.push.model.PushPayload;9 import cn.jpush.api.push.model.audience.Audience;10 import cn.jpush.api.push.model.notification.*;111213 public class JpushClientUtil {1415     private final static String appKey = "此处为appKey";1617     private final static String masterSecret = "此处为masterSecret";1819     private static JPushClient jPushClient = new JPushClient(masterSecret,appKey);2021     /**22      * 推送给设备标识参数的用户23      * @param registrationId 设备标识24      * @param notification_title 通知内容标题25      * @param msg_title 消息内容标题26      * @param msg_content 消息内容27      * @param extrasparam 扩展字段28      * @return 0推送失败,1推送成功29      */30     public static int sendToRegistrationId( String registrationId,String notification_title, String msg_title, String msg_content, String extrasparam) {31         int result = 0;32         try {33             PushPayload pushPayload= JpushClientUtil.buildPushObject_all_registrationId_alertWithTitle(registrationId,notification_title,msg_title,msg_content,extrasparam);34             System.out.println(pushPayload);35             PushResult pushResult=jPushClient.sendPush(pushPayload);36             System.out.println(pushResult);37             if(pushResult.getResponseCode()==200){38                 result=1;39             }40         } catch (APIConnectionException e) {41             e.printStackTrace();4243         } catch (APIRequestException e) {44             e.printStackTrace();45         }4647          return result;48     }4950     /**51      * 发送给所有安卓用户52      * @param notification_title 通知内容标题53      * @param msg_title 消息内容标题54      * @param msg_content 消息内容55      * @param extrasparam 扩展字段56      * @return 0推送失败,1推送成功57      */58     public static int sendToAllAndroid( String notification_title, String msg_title, String msg_content, String extrasparam) {59         int result = 0;60         try {61             PushPayload pushPayload= JpushClientUtil.buildPushObject_android_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);62             System.out.println(pushPayload);63             PushResult pushResult=jPushClient.sendPush(pushPayload);64             System.out.println(pushResult);65             if(pushResult.getResponseCode()==200){66                 result=1;67             }68         } catch (Exception e) {6970             e.printStackTrace();71         }7273          return result;74     }7576     /**77      * 发送给所有IOS用户78      * @param notification_title 通知内容标题79      * @param msg_title 消息内容标题80      * @param msg_content 消息内容81      * @param extrasparam 扩展字段82      * @return 0推送失败,1推送成功83      */84     public static int sendToAllIos(String notification_title, String msg_title, String msg_content, String extrasparam) {85         int result = 0;86         try {87             PushPayload pushPayload= JpushClientUtil.buildPushObject_ios_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);88             System.out.println(pushPayload);89             PushResult pushResult=jPushClient.sendPush(pushPayload);90             System.out.println(pushResult);91             if(pushResult.getResponseCode()==200){92                 result=1;93             }94         } catch (Exception e) {9596             e.printStackTrace();97         }9899          return result;100     }101102     /**103      * 发送给所有用户104      * @param notification_title 通知内容标题105      * @param msg_title 消息内容标题106      * @param msg_content 消息内容107      * @param extrasparam 扩展字段108      * @return 0推送失败,1推送成功109      */110     public static int sendToAll( String notification_title, String msg_title, String msg_content, String extrasparam) {111         int result = 0;112         try {113             PushPayload pushPayload= JpushClientUtil.buildPushObject_android_and_ios(notification_title,msg_title,msg_content,extrasparam);114             System.out.println(pushPayload);115             PushResult pushResult=jPushClient.sendPush(pushPayload);116             System.out.println(pushResult);117             if(pushResult.getResponseCode()==200){118                 result=1;119             }120         } catch (Exception e) {121122             e.printStackTrace();123         }124125         return result;126     }127128129130     public static PushPayload buildPushObject_android_and_ios(String notification_title, String msg_title, String msg_content, String extrasparam) {131         return PushPayload.newBuilder()132                 .setPlatform(Platform.android_ios())133                 .setAudience(Audience.all())134                 .setNotification(Notification.newBuilder()135                         .setAlert(notification_title)136                         .addPlatformNotification(AndroidNotification.newBuilder()137                                 .setAlert(notification_title)138                                 .setTitle(notification_title)139                                 //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)140                                 .addExtra("androidNotification extras key",extrasparam)141                                 .build()142                         )143                         .addPlatformNotification(IosNotification.newBuilder()144                                 //传一个IosAlert对象,指定apns title、title、subtitle等145                                 .setAlert(notification_title)146                                 //直接传alert147                                 //此项是指定此推送的badge自动加1148                                 .incrBadge(1)149                                 //此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,150                                 // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音151                                 .setSound("sound.caf")152                                 //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)153                                 .addExtra("iosNotification extras key",extrasparam)154                                 //此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification155                                 // .setContentAvailable(true)156157                                 .build()158                         )159                         .build()160                 )161                 //Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,162                 // sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的163                 // [通知与自定义消息有什么区别?]了解通知和自定义消息的区别164                 .setMessage(Message.newBuilder()165                         .setMsgContent(msg_content)166                         .setTitle(msg_title)167                         .addExtra("message extras key",extrasparam)168                         .build())169170                 .setOptions(Options.newBuilder()171                         //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义172                         .setApnsProduction(false)173                         //此字段是给开发者自己给推送编号,方便推送者分辨推送记录174                         .setSendno(1)175                         //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒176                         .setTimeToLive(86400)177                         .build()178                 )179                 .build();180     }181182     private static PushPayload buildPushObject_all_registrationId_alertWithTitle(String registrationId,String notification_title, String msg_title, String msg_content, String extrasparam) {183184         System.out.println("----------buildPushObject_all_all_alert");185         //创建一个IosAlert对象,可指定APNs的alert、title等字段186         //IosAlert iosAlert =  IosAlert.newBuilder().setTitleAndBody("title", "alert body").build();187188         return PushPayload.newBuilder()189                 //指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台190                 .setPlatform(Platform.all())191                 //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id192                 .setAudience(Audience.registrationId(registrationId))193                 //jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发194                 .setNotification(Notification.newBuilder()195                         //指定当前推送的android通知196                         .addPlatformNotification(AndroidNotification.newBuilder()197198                                 .setAlert(notification_title)199                                 .setTitle(notification_title)200                                 //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)201                                 .addExtra("androidNotification extras key",extrasparam)202203                                 .build())204                         //指定当前推送的iOS通知205                         .addPlatformNotification(IosNotification.newBuilder()206                                 //传一个IosAlert对象,指定apns title、title、subtitle等207                                 .setAlert(notification_title)208                                 //直接传alert209                                 //此项是指定此推送的badge自动加1210                                 .incrBadge(1)211                                 //此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,212                                 // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音213                                 .setSound("sound.caf")214                                 //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)215                                 .addExtra("iosNotification extras key",extrasparam)216                                 //此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification217                                 //取消此注释,消息推送时ios将无法在锁屏情况接收218                                 // .setContentAvailable(true)219220                                 .build())221222223                         .build())224                 //Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,225                 // sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的226                 // [通知与自定义消息有什么区别?]了解通知和自定义消息的区别227                 .setMessage(Message.newBuilder()228229                         .setMsgContent(msg_content)230231                         .setTitle(msg_title)232233                         .addExtra("message extras key",extrasparam)234235                         .build())236237                 .setOptions(Options.newBuilder()238                         //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义239                         .setApnsProduction(false)240                         //此字段是给开发者自己给推送编号,方便推送者分辨推送记录241                         .setSendno(1)242                         //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天;243                         .setTimeToLive(86400)244245                         .build())246247                 .build();248249     }250251     private static PushPayload buildPushObject_android_all_alertWithTitle(String notification_title, String msg_title, String msg_content, String extrasparam) {252         System.out.println("----------buildPushObject_android_registrationId_alertWithTitle");253         return PushPayload.newBuilder()254                 //指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台255                 .setPlatform(Platform.android())256                 //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id257                 .setAudience(Audience.all())258                 //jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发259                 .setNotification(Notification.newBuilder()260                         //指定当前推送的android通知261                         .addPlatformNotification(AndroidNotification.newBuilder()262                                 .setAlert(notification_title)263                                 .setTitle(notification_title)264                                 //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)265                                 .addExtra("androidNotification extras key",extrasparam)266                                 .build())267                         .build()268                 )269                 //Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,270                 // sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的271                 // [通知与自定义消息有什么区别?]了解通知和自定义消息的区别272                 .setMessage(Message.newBuilder()273                         .setMsgContent(msg_content)274                         .setTitle(msg_title)275                         .addExtra("message extras key",extrasparam)276                         .build())277278                 .setOptions(Options.newBuilder()279                         //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义280                         .setApnsProduction(false)281                         //此字段是给开发者自己给推送编号,方便推送者分辨推送记录282                         .setSendno(1)283                         //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒284                         .setTimeToLive(86400)285                         .build())286                 .build();287     }288289     private static PushPayload buildPushObject_ios_all_alertWithTitle( String notification_title, String msg_title, String msg_content, String extrasparam) {290         System.out.println("----------buildPushObject_ios_registrationId_alertWithTitle");291         return PushPayload.newBuilder()292                 //指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台293                 .setPlatform(Platform.ios())294                 //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id295                 .setAudience(Audience.all())296                 //jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发297                 .setNotification(Notification.newBuilder()298                         //指定当前推送的android通知299                         .addPlatformNotification(IosNotification.newBuilder()300                                 //传一个IosAlert对象,指定apns title、title、subtitle等301                                 .setAlert(notification_title)302                                 //直接传alert303                                 //此项是指定此推送的badge自动加1304                                 .incrBadge(1)305                                 //此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,306                                 // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音307                                 .setSound("sound.caf")308                                 //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)309                                 .addExtra("iosNotification extras key",extrasparam)310                                 //此项说明此推送是一个background推送,想了解background看:311                                // .setContentAvailable(true)312313                                 .build())314                         .build()315                 )316                 //Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,317                 // sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的318                 // [通知与自定义消息有什么区别?]了解通知和自定义消息的区别319                 .setMessage(Message.newBuilder()320                         .setMsgContent(msg_content)321                         .setTitle(msg_title)322                         .addExtra("message extras key",extrasparam)323                         .build())324325                 .setOptions(Options.newBuilder()326                         //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义327                         .setApnsProduction(false)328                         //此字段是给开发者自己给推送编号,方便推送者分辨推送记录329                         .setSendno(1)330                         //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒331                         .setTimeToLive(86400)332                         .build())333                 .build();334     }335336 //    public static void main(String[] args){337 //        if(JpushClientUtil.sendToAllIos("testIos","testIos","this is a ios Dev test","")==1){338 //            System.out.println("success");339 //        }340 //    }341 }342343344345 public class Jdpush {346      protected static final Logger LOG = LoggerFactory.getLogger(Jdpush.class);347348      // demo App defined in resources/jpush-api.conf349350     public static final String TITLE = "申通快递";351     public static final String ALERT = "祝大家新春快乐";352     public static final String MSG_CONTENT = "申通快递祝新老客户新春快乐";353     public static final String REGISTRATION_ID = "0900e8d85ef";354     public static final String TAG = "tag_api";355356     public  static JPushClient jpushClient=null;357358     public static void testSendPush(String appKey ,String masterSecret) {359360361362          jpushClient = new JPushClient(masterSecret, appKey, 3);363364         // HttpProxy proxy = new HttpProxy("localhost", 3128);365         // Can use this https proxy: https://github.com/Exa-Networks/exaproxy 366367368         // For push, all you need do is to build PushPayload object.369         //PushPayload payload = buildPushObject_all_all_alert();370          //生成推送的内容,这里我们先测试全部推送371         PushPayload payload=buildPushObject_all_alias_alert();372373374         try {375             System.out.println(payload.toString());376             PushResult result = jpushClient.sendPush(payload);377             System.out.println(result+"................................");378379             LOG.info("Got result - " + result);380381         } catch (APIConnectionException e) {382             LOG.error("Connection error. Should retry later. ", e);383384         } catch (APIRequestException e) {385             LOG.error("Error response from JPush server. Should review and fix it. ", e);386             LOG.info("HTTP Status: " + e.getStatus());387             LOG.info("Error Code: " + e.getErrorCode());388             LOG.info("Error Message: " + e.getErrorMessage());389             LOG.info("Msg ID: " + e.getMsgId());390         }391     }392393     public static PushPayload buildPushObject_all_all_alert() {394         return PushPayload.alertAll(ALERT);395     }396397     public static PushPayload buildPushObject_all_alias_alert() {398         return PushPayload.newBuilder()399                 .setPlatform(Platform.all())//设置接受的平台400                 .setAudience(Audience.all())//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到401                 .setNotification(Notification.alert(ALERT))402                 .build();403     }404405     public static PushPayload buildPushObject_android_tag_alertWithTitle() {406         return PushPayload.newBuilder()407                 .setPlatform(Platform.android())408                 .setAudience(Audience.all())409                 .setNotification(Notification.android(ALERT, TITLE, null))410                 .build();411     }412413     public static PushPayload buildPushObject_android_and_ios() {414         return PushPayload.newBuilder()415                 .setPlatform(Platform.android_ios())416                 .setAudience(Audience.tag("tag1"))417                 .setNotification(Notification.newBuilder()418                         .setAlert("alert content")419                         .addPlatformNotification(AndroidNotification.newBuilder()420                                 .setTitle("Android Title").build())421                         .addPlatformNotification(IosNotification.newBuilder()422                                 .incrBadge(1)423                                 .addExtra("extra_key", "extra_value").build())424                         .build())425                 .build();426     }427428     public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {429         return PushPayload.newBuilder()430                 .setPlatform(Platform.ios())431                 .setAudience(Audience.tag_and("tag1", "tag_all"))432                 .setNotification(Notification.newBuilder()433                         .addPlatformNotification(IosNotification.newBuilder()434                                 .setAlert(ALERT)435                                 .setBadge(5)436                                 .setSound("happy")437                                 .addExtra("from", "JPush")438                                 .build())439                         .build())440                  .setMessage(Message.content(MSG_CONTENT))441                  .setOptions(Options.newBuilder()442                          .setApnsProduction(true)443                          .build())444                  .build();445     }446447     public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {448         return PushPayload.newBuilder()449                 .setPlatform(Platform.android_ios())450                 .setAudience(Audience.newBuilder()451                         .addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))452                         .addAudienceTarget(AudienceTarget.alias("alias1", "alias2"))453                         .build())454                 .setMessage(Message.newBuilder()455                         .setMsgContent(MSG_CONTENT)456                         .addExtra("from", "JPush")457                         .build())458                 .build();459     }460 }  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: