I am using the Java-APNS framework to send push notifications to iOS devices. It works very well (thanks to the developer!) and I am able to send to my devices when everything is set up correctly. I would like, however, to properly handle fail cases. There are some documented APNS error codes that should be received for certain cases:
http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW1
How would I create some of the fail cases? I have my own class that implements ApnsDelegate and construct my ApnsService object with my delegate. I've tried passing an invalid token in my call to push as well as an empty string for payload:
service.push("ab", "")
but the messageSent method of my ApnsDelegate still gets called. I would expect this to end up in the messageSendFailed method instead or maybe the connectionClosed but neither gets called. Has anyone tested this stuff and gotten it to work? Here is an example of my class...I'm actually doing this in Scala but figured I would have better luck if I posted Java. When called, this will just print "Message sent to AB".
public class ApnsSender implements ApnsDelegate {
public void send(Notification Notification) {
ApnsService service = getApnsService();
String payload = getPayload(notification);
service.push("ab", "");
}
private ApnsService getApnsService() { ... }
private String getPayload(Notification notification) { ... }
public void messageSent(ApnsNotification message) {
String token = Utilities.toHexString(message.getDeviceToken());
System.out.println("Message sent to " + token);
}
public void messageSendFailed(ApnsNotification message, Throwable e) {
System.out.println("Message failed");
}
public void messageSent(DeliveryError e, Int messageIdentifier) {
System.out.println("Message closed with error code " + e.code());
}
}
As mentioned in my comment, I looked into JavaPNS as an alternative and this is a much better framework to use. It's also being currently worked on (last commit 2 days ago at time of posting) where as java-apns hasn't been updated since December of 2011.
I suggest that use of java-apns be replaced with use of JavaPNS for anyone trying to send push notifications to iOS devices. It reacts to error packets received and passes the results on to the caller in an easy to access fashion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With