diff --git a/src/main/java/com/dbay/apns4j/impl/ApnsConnectionImpl.java b/src/main/java/com/dbay/apns4j/impl/ApnsConnectionImpl.java index 4c0dd50..66a05e3 100644 --- a/src/main/java/com/dbay/apns4j/impl/ApnsConnectionImpl.java +++ b/src/main/java/com/dbay/apns4j/impl/ApnsConnectionImpl.java @@ -182,12 +182,7 @@ public void sendNotification(PushNotification notification) { notificationCachedQueue.add(notification); lastSuccessfulTime = System.currentTimeMillis(); - - /** TODO there is a bug, maybe, theoretically. - * CN: 假如我们发了一条错误的通知,然后又发了 maxCacheLength 条正确的通知。这时APNS服务器 - * 才返回第一条通知的error-response。此时,第一条通知已经从队列移除了。。 - * 其实后面100条该重发,但却没有。不过这个问题的概率很低,我们还是信任APNS服务器能及时返回 - */ + if (notificationCachedQueue.size() > maxCacheLength) { notificationCachedQueue.poll(); } @@ -269,7 +264,7 @@ public void run() { /** EN: error-response,close the socket and resent notifications * CN: 一旦遇到错误返回就关闭连接,并且重新发送在它之后发送的通知 */ - if (size == res.length && command == Command.ERROR) { + if (size == res.length && (command == Command.INVALID_TOKEN || command == Command.SHUTDOWN)) { int status = res[1]; int errorId = ApnsTools.parse4ByteInt(res[2], res[3], res[4], res[5]); @@ -298,6 +293,10 @@ public void run() { } } if (!found) { + /* It means the error one already out of the queue, all notification should add in resent queue. */ + resentQueue.addAll(notificationCachedQueue); + notificationCachedQueue.clear(); + /* Maybe there should auto increase maxCacheLength . */ logger.warn(connName + " Didn't find error-notification in the queue. Maybe it's time to adjust cache length. id: " + errorId); } } diff --git a/src/main/java/com/dbay/apns4j/model/Command.java b/src/main/java/com/dbay/apns4j/model/Command.java index 17d3685..e7f99f4 100644 --- a/src/main/java/com/dbay/apns4j/model/Command.java +++ b/src/main/java/com/dbay/apns4j/model/Command.java @@ -25,5 +25,7 @@ public class Command { public static final int SEND_V2 = 2; - public static final int ERROR = 8; + public static final int INVALID_TOKEN = 8; + + public static final int SHUTDOWN = 10; }