java 基于javapns IOS推送的配置
1.enable logging javapns使用的log4j,为确保log的正常工作,在使用过程中添加如下代码: import org.apache.log4j.*; ... try { BasicConfigurator.configure(); ... } catch (Exception e) { //do sth. } log4j.properties中添加: log4j.logger.javapns=debug 2.sandbox(开发环境)到production(产品环境)迁移的注意事项 两套环境的device tokens是不同的,在迁移后需要更新device tokens。 两套环境的certificates也是不同的,需要更新证书。 修复sandbox环境工作,production推送失败的解决方案。 http://www.techjini.com/blog/how-we-fixed-production-push-notifications-not-working-while-sandbox-works/ 3.定制消息内容 public void send (List<Device> devices, Object keystore, String password, boolean production) { /* Build a blank payload to customize */ PushNotificationPayload payload = PushNotificationPayload.complex(); /* Customize the payload */ payload.addAlert("Hello World!"); //apple提示消息 payload.addCustomDictionary("webview", "http://www.womai.com"); //隐藏参数,用于实现一些定制操作,比如自动跳转。 payload.addCustomDictionary("mykey2", 2); // etc. /* Push your custom payload */ List<PushedNotification> notifications = Push.payload(payload, keystore, password, production, devices); } 4.多线程批量发送推送消息 public void send (List<Device> devices, Object keystore, String password, boolean production) { /* Prepare a simple payload to push */ PushNotificationPayload payload = PushNotificationPayload.alert("Hello World!"); /* Decide how many threads you want to create and use */ int threads = 30; /* Start threads, wait for them, and get a list of all pushed notifications */ List<PushedNotification> notifications = Push.payload(payload, keystore, password, production, threads, devices); } 备注:以上多线程方法在所有线程执行完后返回,如果不想等线程执行完毕,可以通过在内部创建一个单独的线程: (示例: new Thread() {public void run() {...}}.start();). 5.创建消息队列(连接池) 一个队列就是一组连接APNS的多线程连接,队列会动态将消息分发到不同的线程中去。 public void send (String token, Object keystore, String password, boolean production) { /* Prepare a simple payload to push */ PushNotificationPayload payload = PushNotificationPayload.alert("Hello World!"); /* Decide how many threads you want your queue to use */ int threads = 30; /* Create the queue */ PushQueue queue = Push.queue(keystore, password, production, threads); /* Start the queue (all threads and connections and initiated) */ queue.start(); /* Add a notification for the queue to push */ queue.add(payload, token); } 备注:如果不通过queue.start消息队列,队列会在第一次调用queue.add 的时候启动 5.建议开启EnhancedNotificationFormat 默认为开启状态,建议开启,开启后可以通过Payload对象获取到更多推送消息的详细信息,例如执行状态,失效时间等。 6.推送状态(错误)管理 1) error-response packets pushedNotification.isSuccessful() //判断是否推送成功 pushedNotification.getException() //获取详细错误信息,系统中错误不会抛出,以保证多线程的正常运作,错误信息会记录到pushedNotification中。 示例代码: List<PushedNotification> notifications = Push.alert("Hello World!", "keystore.p12", "keystore_password", false, devices); for (PushedNotification notification : notifications) { if (notification.isSuccessful()) { /* Apple accepted the notification and should deliver it */ System.out.println("Push notification sent successfully to: " + notification.getDevice().getToken()); /* Still need to query the Feedback Service regularly */ } else { String invalidToken = notification.getDevice().getToken(); /* Add code here to remove invalidToken from your database */ /* Find out more about what the problem was */ Exception theProblem = notification.getException(); theProblem.printStackTrace(); /* If the problem was an error-response packet returned by Apple, get it ResponsePacket theErrorResponse = notification.getResponse(); if (theErrorResponse != null) { System.out.println(theErrorResponse.getMessage()); } } } 2)Feedback Service public class FeedbackTest { public static void main(String[] args) { List<Device> inactiveDevices = Push.feedback("keystore.p12", "keystore_password", false); /* remove inactive devices from your own list of devices */ } 备注:Sandbox Feedback Service not listing device after app is removed Delays involving the Feedback service
文章来自:http://hymcn.blog.51cto.com/9637692/1581689