標籤:javapns;ios;push
1.enable loggingjavapns使用的log4j,為確保log的正常工作,在使用過程中添加如下代碼:import org.apache.log4j.*; ...try { BasicConfigurator.configure(); ... } catch (Exception e) { //do sth. }log4j.properties中添加:log4j.logger.javapns=debug2.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
java 基於javapns IOS推送的配置