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(開發(fā)環(huán)境)到production(產(chǎn)品環(huán)境)遷移的注意事項
兩套環(huán)境的device tokens是不同的,在遷移后需要更新device tokens。
兩套環(huán)境的certificates也是不同的,需要更新證書。
修復sandbox環(huán)境工作,production推送失敗的解決方案。
http://www.techjini.com/blog/how-we-fixed-production-push-notifications-not-working-while-sandbox-works/
3.定制消息內(nèi)容
public void send (List 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"); //隱藏參數(shù),用于實現(xiàn)一些定制操作,比如自動跳轉。
payload.addCustomDictionary("mykey2", 2); // etc.
/* Push your custom payload */
List notifications = Push.payload(payload, keystore, password, production, devices);
}
4.多線程批量發(fā)送推送消息
public void send (List 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 notifications = Push.payload(payload, keystore, password, production, threads, devices);
}
備注:以上多線程方法在所有線程執(zhí)行完后返回,如果不想等線程執(zhí)行完畢,可以通過在內(nèi)部創(chuàng)建一個單獨的線程:
(示例: new Thread() {public void run() {...}}.start();).
5.創(chuàng)建消息隊列(連接池)
一個隊列就是一組連接APNS的多線程連接,隊列會動態(tài)將消息分發(fā)到不同的線程中去。
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消息隊列,隊列會在第一次調(diào)用queue.add 的時候啟動
5.建議開啟EnhancedNotificationFormat
默認為開啟狀態(tài),建議開啟,開啟后可以通過Payload對象獲取到更多推送消息的詳細信息,例如執(zhí)行狀態(tài),失效時間等。
6.推送狀態(tài)(錯誤)管理
1) error-response packets
pushedNotification.isSuccessful() //判斷是否推送成功
pushedNotification.getException() //獲取詳細錯誤信息,系統(tǒng)中錯誤不會拋出,以保證多線程的正常運作,錯誤信息會記錄到pushedNotification中。
示例代碼:
List 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 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基于javapnsIOS推送的配置
鏈接URL:
http://weahome.cn/article/jdjcid.html