溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java 基于javapns IOS推送的配置

發(fā)布時(shí)間:2020-05-30 19:44:29 來源:網(wǎng)絡(luò) 閱讀:586 作者:idalang 欄目:移動開發(fā)
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)境)遷移的注意事項(xiàng)

    兩套環(huán)境的device tokens是不同的,在遷移后需要更新device tokens。
    兩套環(huán)境的certificates也是不同的,需要更新證書。
    修復(fù)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<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"); //隱藏參數(shù),用于實(shí)現(xiàn)一些定制操作,比如自動跳轉(zhuǎn)。   
payload.addCustomDictionary("mykey2", 2);     // etc.  
      /* Push your custom payload */        
List<PushedNotification> notifications = Push.payload(payload, keystore, password, production, devices);  
}


4.多線程批量發(fā)送推送消息
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);  
}
備注:以上多線程方法在所有線程執(zhí)行完后返回,如果不想等線程執(zhí)行完畢,可以通過在內(nèi)部創(chuàng)建一個單獨(dú)的線程:
 (示例: new Thread() {public void run() {...}}.start();). 
 
5.創(chuàng)建消息隊(duì)列(連接池)
一個隊(duì)列就是一組連接APNS的多線程連接,隊(duì)列會動態(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消息隊(duì)列,隊(duì)列會在第一次調(diào)用queue.add 的時(shí)候啟動
 
5.建議開啟EnhancedNotificationFormat
默認(rèn)為開啟狀態(tài),建議開啟,開啟后可以通過Payload對象獲取到更多推送消息的詳細(xì)信息,例如執(zhí)行狀態(tài),失效時(shí)間等。
  
6.推送狀態(tài)(錯誤)管理
1) error-response packets 
    pushedNotification.isSuccessful() //判斷是否推送成功 
    pushedNotification.getException() //獲取詳細(xì)錯誤信息,系統(tǒng)中錯誤不會拋出,以保證多線程的正常運(yùn)作,錯誤信息會記錄到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
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI