java 基于javapns IOS推送的配置
1.enableloggingjavapns使用的log4j,为确保log的正常工作,在使用过程中添加如下代码:importorg.apache.log4j.*;...try{BasicConfigurator.configure();...}catch(Exceptione){//dosth.}log4j.properties中添加:log4j.logger.javapns=debug2.sandbox(开发环境)到production(产品环境)迁移的注意事项两套环境的devicetokens是不同的,在迁移后需要更新devicetokens。两套环境的certificates也是不同的,需要更新证书。修复sandbox环境工作,production推送失败的解决方案。http://www.techjini.com/blog/how-we-fixed-production-push-notifications-not-working-while-sandbox-works/3.定制消息内容publicvoidsend(List<Device>devices,Objectkeystore,Stringpassword,booleanproduction){/*Buildablankpayloadtocustomize*/PushNotificationPayloadpayload=PushNotificationPayload.complex();/*Customizethepayload*/payload.addAlert("HelloWorld!");//apple提示消息payload.addCustomDictionary("webview","http://www.womai.com");//隐藏参数,用于实现一些定制操作,比如自动跳转。payload.addCustomDictionary("mykey2",2);//etc./*Pushyourcustompayload*/List<PushedNotification>notifications=Push.payload(payload,keystore,password,production,devices);}4.多线程批量发送推送消息publicvoidsend(List<Device>devices,Objectkeystore,Stringpassword,booleanproduction){/*Prepareasimplepayloadtopush*/PushNotificationPayloadpayload=PushNotificationPayload.alert("HelloWorld!");/*Decidehowmanythreadsyouwanttocreateanduse*/intthreads=30;/*Startthreads,waitforthem,andgetalistofallpushednotifications*/List<PushedNotification>notifications=Push.payload(payload,keystore,password,production,threads,devices);}备注:以上多线程方法在所有线程执行完后返回,如果不想等线程执行完毕,可以通过在内部创建一个单独的线程:(示例:newThread(){publicvoidrun(){...}}.start();).5.创建消息队列(连接池)一个队列就是一组连接APNS的多线程连接,队列会动态将消息分发到不同的线程中去。publicvoidsend(Stringtoken,Objectkeystore,Stringpassword,booleanproduction){/*Prepareasimplepayloadtopush*/PushNotificationPayloadpayload=PushNotificationPayload.alert("HelloWorld!");/*Decidehowmanythreadsyouwantyourqueuetouse*/intthreads=30;/*Createthequeue*/PushQueuequeue=Push.queue(keystore,password,production,threads);/*Startthequeue(allthreadsandconnectionsandinitiated)*/queue.start();/*Addanotificationforthequeuetopush*/queue.add(payload,token);}备注:如果不通过queue.start消息队列,队列会在第一次调用queue.add的时候启动5.建议开启EnhancedNotificationFormat默认为开启状态,建议开启,开启后可以通过Payload对象获取到更多推送消息的详细信息,例如执行状态,失效时间等。6.推送状态(错误)管理1)error-responsepacketspushedNotification.isSuccessful()//判断是否推送成功pushedNotification.getException()//获取详细错误信息,系统中错误不会抛出,以保证多线程的正常运作,错误信息会记录到pushedNotification中。示例代码:List<PushedNotification>notifications=Push.alert("HelloWorld!","keystore.p12","keystore_password",false,devices);for(PushedNotificationnotification:notifications){if(notification.isSuccessful()){/*Appleacceptedthenotificationandshoulddeliverit*/System.out.println("Pushnotificationsentsuccessfullyto:"+notification.getDevice().getToken());/*StillneedtoquerytheFeedbackServiceregularly*/}else{StringinvalidToken=notification.getDevice().getToken();/*AddcodeheretoremoveinvalidTokenfromyourdatabase*//*Findoutmoreaboutwhattheproblemwas*/ExceptiontheProblem=notification.getException();theProblem.printStackTrace();/*Iftheproblemwasanerror-responsepacketreturnedbyApple,getitResponsePackettheErrorResponse=notification.getResponse();if(theErrorResponse!=null){System.out.println(theErrorResponse.getMessage());}}}2)FeedbackServicepublicclassFeedbackTest{publicstaticvoidmain(String[]args){List<Device>inactiveDevices=Push.feedback("keystore.p12","keystore_password",false);/*removeinactivedevicesfromyourownlistofdevices*/}备注:SandboxFeedbackServicenotlistingdeviceafterappisremovedDelaysinvolvingtheFeedbackservice
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。