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

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

问题描述

我正在尝试在 Java 中实现新的命令 2"推送通知,但不能让它推送多个警报.成功推送第一个警报.如果您能发现此代码的任何问题,请帮助

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

苹果规格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 };
}

推荐答案

由于您从 APNS 返回错误代码,因此此时应断开连接,APNS 将忽略错误后的所有内容.当您收到错误返回时,标识符是您当前使用随机数的标识符.

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.

这里没有简单的解决方案——你必须重新构建你所拥有的东西,这样当你收到错误时,你可以找出在那之后的所有内容并重新发送——我建议使用一个序列号作为标识符,然后将数据包存储在您定期清除的队列中(您必须将它们保留 30 秒以确保 Apple 接受它们).

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 Apple 推送通知不发送多个警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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