Skip to content

Instantly share code, notes, and snippets.

@yongboy
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yongboy/9635763 to your computer and use it in GitHub Desktop.
Save yongboy/9635763 to your computer and use it in GitHub Desktop.
Invalid Token will lead to left message send fail within the same thread.
package com.push.test;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import com.notnoop.apns.APNS;
import com.notnoop.apns.ApnsDelegate;
import com.notnoop.apns.ApnsNotification;
import com.notnoop.apns.ApnsService;
import com.notnoop.apns.ApnsServiceBuilder;
import com.notnoop.apns.DeliveryError;
import com.notnoop.apns.ReconnectPolicy.Provided;
import com.notnoop.apns.internal.ReconnectPolicies.Always;
import com.notnoop.apns.internal.Utilities;
/**
* 在一次批量发送时,Invald Token会导致发送失败,隶属于当前线程的其它后续消息都会造成发送失败。<br/>
* java-apns 0.2.*代码暂无解决以上问题,原则:只要发送失败,就应该马上关闭当前连接,在其它任务之前
*
* 已修改版java-apns,检出地址见:https://github.com/yongboy/java-apns
*
* @author nieyong
*
*/
public class ButchSenderTest {
// itouch's
private static String TOKEN = "fae0d6dc34bc37d09d80d1ffbf0b66133623d85662c74e344a616702e3bb0096";
// wrong token
private static String BAD_TOKEN = "fae0d6dc34bc37d09d80d1ffbf0b66133623d85662c74e344a616702e3bb0095";
private static String BAD_CHEN_TOKEN = "333a1c5585f88554a23f1fe7f7c6ce3f3a891b958291eac699accdad5eb6268b";
private static String BAD_CHEN_TOKEN_2 = "444a1c5585f88554a23f1fe7f7c6ce3f3a891b958291eac699accdad5eb6268b";
public static void main(String[] args) throws InterruptedException {
// youku ios's
String password = "your password here ...";
String pemClassPath = "your pem classpath here ..."; // eg: conf/your.p12
butchSendMessages(pemClassPath, password);
// singleSendMessages(pemClassPath, password);
}
private static void singleSendMessages(String pemClassPath, String password)
throws InterruptedException {
ApnsServiceBuilder builder = APNS
.newService()
.withReconnectPolicy(new Always())
.withCert(ClassLoader.getSystemResourceAsStream(pemClassPath),
password).withDelegate(new ApnsDelegateImpl())
.withReconnectPolicy(Provided.EVERY_HALF_HOUR);
ApnsService service = builder.withProductionDestination().asPool(2)
.build();
String payload = APNS.newPayload().badge(1)
.alertBody("Btuch send with in invalid token, receive it ?").build();
service.push(BAD_TOKEN, payload); // failure
service.push(BAD_CHEN_TOKEN, payload); // failure
service.push(BAD_CHEN_TOKEN_2, payload); // failure
service.push(TOKEN, payload);
System.out.println("Done!\nsleep 6000s just for waiting ...");
Thread.sleep(6000L * 1000L);
System.out.println("going to die now ...");
service.stop();
System.out.println("end!");
}
private static void butchSendMessages(String pemClassPath, String password)
throws InterruptedException {
ApnsServiceBuilder builder = APNS
.newService()
.withReconnectPolicy(new Always())
.withCert(ClassLoader.getSystemResourceAsStream(pemClassPath),
password).withDelegate(new ApnsDelegateImpl())
.withReconnectPolicy(Provided.EVERY_HALF_HOUR);
ApnsService service = builder.withProductionDestination().asPool(2)
.build();
String payload = APNS.newPayload().badge(1)
.alertBody("Btuch send with in invalid token, receive it ?").build();
// 批量发送
List<String> tokens = new LinkedList<String>();
tokens.add(BAD_TOKEN);
tokens.add(BAD_CHEN_TOKEN);
tokens.add(BAD_CHEN_TOKEN_2);
tokens.add(TOKEN);
service.push(tokens, payload);
System.out.println("Done!\nsleep 6000s just for waiting ...");
Thread.sleep(6000L * 1000L);
System.out.println("going to die now ...");
service.stop();
System.out.println("end!");
}
}
class ApnsDelegateImpl implements ApnsDelegate {
@Override
public void messageSendFailed(ApnsNotification notification, Throwable arg1) {
System.out.println(getNow() + " FAIL!!");
arg1.printStackTrace();
}
@Override
public void connectionClosed(DeliveryError e, int messageIdentifier) {
if (e.code() == 8 || messageIdentifier == -1) {
// System.out.println("e.code == " + e.code());
// first find the token by messageIdentifier
// next add the token to black list
// ..........
}
System.out.println(getNow() + " CLOSED!! " + e.toString()
+ "e.code == " + e.code() + " :messageIdentifier="
+ messageIdentifier);
}
@Override
public void cacheLengthExceeded(int arg0) {
System.out.println("cacheLengthExceeded with " + arg0);
}
@Override
public void messageSent(ApnsNotification notification, boolean arg1) {
System.out.println(getNow() + " SEND!! Token "
+ Utilities.encodeHex(notification.getDeviceToken()));
}
@Override
public void notificationsResent(int resendNum) {
System.out.println("resendNum = " + resendNum);
System.out.println(getNow() + " RESENT");
}
private String getNow() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String now = dateFormat.format(new Date());
return now + " " + Thread.currentThread().toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment