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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。