javaPNS處理APNS返回結果

 javaPNS處理APNS返回結果
 當我們通過javaPNS發送通知給APNS後,如果token失效或者程序已被目標用戶刪除等原因可能導致發送失敗。通過javaPNS返回的結果我們可以處理保存的設備token來防止下次發送給無效的設備。
 APNS有兩種不同的錯誤報告系統,這兩種系統工作方式不同而且報告的錯誤種類也不同。第一種是 — error-response packets —當我們通過javaPNS發送通知後立即可以從PushNotification中獲取
 到,另一種是Feedback Service,這個需要我們定期從Apple那裏獲取失效的設備信息列表。我們不能只依靠一種方式來處理失效設備信息,必須兩種方式結合使用。
 
 第一種方式  從返回的PushNotification中獲取錯誤信息。
 通過isSuccessful()我們可以獲知發送是否成功,如果沒有成功則通過調用pushedNotification.getException()來獲取失敗的原因
 通過(exception.printStackTrace())打印異常。這裏需要注意:此異常並不會拋出,而是存在於PushedNotification中。
 如果錯誤是唄Apple服務器報告的error-response packet,我們可以通過pushedNotification.getResponse().方法獲取到。
 下面是具體的操作步驟:
 import javapns.*;
 import javapns.notification.*;
        
 public class PushTest {
       public static void main(String[] args) {
               String[] devices = {"MyToken111111111111111111111111111111111111111111111111111111111", 
                                   "MyToken222222222222222222222222222222222222222222222222222222222"}; 
               try {
                        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());
                                        }
                                }
                        }
                        
                } catch (KeystoreException e) {
                        /* A critical problem occurred while trying to use your keystore */  
                        e.printStackTrace();
                        
                } catch (CommunicationException e) {
                        /* A critical communication error occurred while trying to contact Apple servers */  
                        e.printStackTrace();
                }
        }
 }
 

 第二種方式 通過Feedback Service方式
 javaPNS爲我們提供了通過Feedback Service獲取失效設備列表的一切。首先來看代碼:
  import javapns.*;
        
 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 */ 
    }
 }

 關於feedback的參數也像之前Push其他方法的參數一樣。我們需要定期調用此方法來獲取失效的設備並做相應的處理。關於feedback service還有幾點小知識:並不能
 通過 經過多少次信息推送失敗或者從設備被標記爲不活躍(inactive)狀態多久 這些信息來計算此token是否被加入到feedback service中,因爲Apple有非常多的
 參數來決定是否將某個設備放入到feedback Service中。如果一個設備不能推送過去通知,可能是網絡原因,設備故障或者配置錯誤等等其他原因,這些原因
 都可能導致設備暫時的不活躍(inactive),但是這些都是暫時的。   另外如果程序被刪除了,那麼該程序會直接被放到feedback service中,但是這有一個前提:
 就是設備中有其他的軟件可以與APNS交互,這樣它可以通知Apple Server某個程序被刪除了,如果你的程序是該iphone裏最後一個可以與APNS交互的程序,那麼當你的
 程序被刪除時,沒有辦法通知Apple server了,那麼從feedback service中就無法獲取到該設備了。
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章