新命令2苹果推送通知不发送多个警报 [英] New Command 2 Apple Push Notification Not sending multiple alerts

查看:133
本文介绍了新命令2苹果推送通知不发送多个警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实施新的命令2推Java的通知,不能有它推多个警报。第一个警报被成功推。请帮助,如果你可以在此code发现任何问题。

苹果规格
<一href=\"https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW1\" rel=\"nofollow\">https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW1

 为(DeviceApps deviceApps:deviceAppsList){
outputStream.write(getByteArray(deviceApps,pushAlert));
}
私人字节[] getByteArray(DeviceApps deviceApps,PushAlert pushAlert){ByteArrayOutputStream dataBao =新ByteArrayOutputStream();
//写TokenLength为16位无符号整型,大端
dataBao.write((字节)1);
 dataBao.write(intTo2ByteArray(32));
dataBao.write(deviceTokenAsBytes);//写PayloadLength为16位无符号整型,大端
 dataBao.write((字节)2);
dataBao.write(intTo2ByteArray(payLoadAsBytes.length));
dataBao.write(payLoadAsBytes);// 4字节。通知标识符
dataBao.write((字节3)3);
dataBao.write(intTo2ByteArray(4));
dataBao.write(intTo4ByteArray(random.nextInt()));// 4字节截止日期
dataBao.write((字节)4);
dataBao.write(intTo2ByteArray(4));
dataBao.write(intTo4ByteArray(pushAlert.getUtcExpireTime()));
LOG.error(UtcExpireTime =+ pushAlert.getUtcExpireTime());// 1字节优先
dataBao.write((字节)5);
dataBao.write(intTo2ByteArray(1));
dataBao.write((字节)10);
//帧信息
宝=新ByteArrayOutputStream();
bao.write((字节)2);
字节[]数据= dataBao.toByteArray();
bao.write(intTo4ByteArray(data.length));
LOG.error(data.length+ data.length);
bao.write(数据);返回bao.toByteArray();
}
支持方法
私有静态最后一个字节[] intTo4ByteArray(int值){
返回ByteBuffer.allocate(4).putInt(值).array();
}私有静态最后一个字节[] intTo2ByteArray(int值){
INT S1 =(价值&安培;为0xFF00)GT;&GT; 8;
    INT S2 =价值和放大器; 0xFF的;
    返回新的字节[] {(字节)S1(字节)S2};
}


解决方案

既然你是从APNS取回一个错误code,连接应在该点下降,APNS会忽略错误后的一切。当您收到一个错误回来,标识符是你目前正在使用一个随机数的标识符。

有没有简单的解决方案,在这里 - 你必须重新构建你所拥有的,这样当您收到错误,您可以在该点之后找出的一切,然后重新发送 - 我建议使用序列号的标识,然后存储您定期清除队列中的数据包(你必须保持他们周围说30秒,以保证苹果接受它们)。

I am trying to implement the new 'Command 2' push notification in Java and cannot have it push multiple alerts. First alert is pushed successfully. Please help if you can spot any issue on this code

Apple specs https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW1

for (DeviceApps deviceApps : deviceAppsList) {
outputStream.write(getByteArray(deviceApps, pushAlert));
}


private byte[] getByteArray(DeviceApps deviceApps, PushAlert pushAlert) {

ByteArrayOutputStream dataBao = new ByteArrayOutputStream();
// Write the TokenLength as a 16bits unsigned int, in big endian
dataBao.write((byte)1);
 dataBao.write(intTo2ByteArray(32));
dataBao.write(deviceTokenAsBytes);

// Write the PayloadLength as a 16bits unsigned int, in big endian
 dataBao.write((byte)2);
dataBao.write(intTo2ByteArray(payLoadAsBytes.length));
dataBao.write(payLoadAsBytes);

// 4 bytes. Notification identifier
dataBao.write((byte)3);
dataBao.write(intTo2ByteArray(4));
dataBao.write(intTo4ByteArray(random.nextInt()));

// 4 bytes Expiration date
dataBao.write((byte)4);
dataBao.write(intTo2ByteArray(4));
dataBao.write(intTo4ByteArray(pushAlert.getUtcExpireTime()));
LOG.error("UtcExpireTime="+ pushAlert.getUtcExpireTime());

// 1 bytes Priority
dataBao.write((byte)5);
dataBao.write(intTo2ByteArray(1));
dataBao.write((byte)10);


//Frame Info
bao = new ByteArrayOutputStream();
bao.write((byte)2);
byte [] data = dataBao.toByteArray();
bao.write(intTo4ByteArray(data.length));
LOG.error(" data.length "+data.length);
bao.write(data);

return bao.toByteArray();               
}


Support Methods
private static final byte[] intTo4ByteArray(int value) {
return ByteBuffer.allocate(4).putInt(value).array();
}

private static final byte[] intTo2ByteArray(int value) {
int s1 = (value & 0xFF00) >> 8;
    int s2 = value & 0xFF;
    return new byte[] { (byte) s1, (byte) s2 };
}

解决方案

Since you are getting back a an error code from APNS, the connection should be dropped at that point and APNS will ignore everything after the error. When you receive an error back, the identifier is the identifier you are currently using a random number for.

There's no easy solution here -- you have to rearchitect what you have so that when you receive the error, you can figure out everything after that point and resend -- I'd recommend using a sequential number for the Identifier and then storing the packets in a queue that you purge periodically (you have to keep them around for say 30 seconds to guarantee that Apple Accepted them).

这篇关于新命令2苹果推送通知不发送多个警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆