我使用notnoop / java-apns库将消息从我的服务器推送到IOS设备。 推送工作正常,消息到达客户端。
我试图检测邮件是否被推送到客户端,但是 问题是我无法使用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 :(得分: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();