2015-05-26 67 views
0

我正在使用notnoop/java-apns庫將消息從我的服務器推送到IOS設備。 推送正常,消息到達客戶端。notnoop/java-apns無法檢測到錯誤

我試圖檢測消息是否被推送到客戶端,但是 問題是我無法使用EnhancedApnsNotification檢測推送消息的錯誤/成功並實現了ApnsDelegate類。

的messageSendFailed & messageSent不叫

我的代碼

public class PushTest implements ApnsDelegate { 
private static pushtest instance = null; 

private static ApnsService service; 
private static int counter= 0; 

private pushtest() { 
} 

static public PushTest instance() { 
    if (instance == null) { 
     instance = new pushtest(); 
     try { 
      service = APNS.newService() 
        .withCert("certPath", "password") 
        .withSandboxDestination() 
        .build();  
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
    return instance; 
} 

public void send(String token) { 
    LinkedHashMap<String, Boolean> result = new LinkedHashMap<String, Boolean>(); 
    // build payload 
    int now = (int)(new Date().getTime()/1000); 

    try { 
     String payload = APNS.newPayload() 
       .badge(3) 
       .alertBody("test") 
       .build(); 
    EnhancedApnsNotification notification = new EnhancedApnsNotification(counter++, 
     now + 60 * 60 /* Expire in one hour */, 
     token /* Device Token */, 
     payload); 
    service.push(notification);  
    } catch (Exception e1) { 
     System.out.println("IosPush :" + e1.getMessage()); 
    } 
} 
@Override 
public void connectionClosed(DeliveryError arg0, int arg1) { 
    System.out.println("connectionClosed");  
} 
@Override 
public void messageSendFailed(ApnsNotification arg0, Throwable arg1) { 
    System.out.println("messageSendFailed"); 
} 
@Override 
public void messageSent(ApnsNotification arg0) { 
    System.out.println("messageSent"); 
} 

}

回答

0

我覺得只是代表未連接到服務。創建一個委託,然後使用構建器的withDelegate(..)方法將其連接起來。

public class MyApnsDelegate extends ApnsDelegateAdapter { 


    Logger log = LoggerFactory.getLogger(MyApnsDelegate.class); 

    @Override 
    public void messageSent(ApnsNotification apnsNotification, boolean b) 
    { 
     log.info("MyApnsDelegate called!"); 
    } 
} 

然後在樹立APNS服務的代碼,APNS.newService()返回一個ApnsServiceBuilder它可以讓你設定的委託:

service = APNS.newService() 
       .withCert("certPath", "password") 
       .withSandboxDestination() 
       .withDelegate(new MyApnsDelegate()) 
       .build();