【发布时间】:2015-02-27 01:52:06
【问题描述】:
查看下面的编辑,我发现问题出在 c# 代码上,但无法找到!
我知道以前最大有效负载大小为 256b,但现在使用 ios8 我们可以发送高达 2kb 的有效负载,但它不适合我。
这里说: APN (Apple Push Notification) payload size limit
例子:
如果我发送此有效负载工作正常(我收到通知):
{"aps":{"alert":"New negotiation opened from (idOperation: 5) pablo.gomez","sound":"default"},"data":{"notificationType":"2","idOperation":"5","link":"http://localhost:7000/Login.aspx?idInstance=9&forward=VE9fVHJhZGluZy5hc3B4P2lkT3BlcmF0aW9uPTU="}}
但如果我再发送一个,它就不起作用(我的手机上没有收到),例如:
"{"aps":{"alert":"New negotiation test test test test test test test opened from (idOperation: 5) pablo.gomez","sound":"default"},"data":{"notificationType":"2","idOperation":"5","link":"http://localhost:7000/Login.aspx?idInstance=9&forward=VE9fVHJhZGluZy5hc3B4P2lkT3BlcmF0aW9uPTU="}}"
我也在开发和生产中尝试过这个。第二个都失败了。我已经用 iPhone 5 iOS 8.1.3 对此进行了测试
我的应用的 iOS 部署目标是 iOS 8.0,但我上传到商店的版本部署目标是 iOS 7.0,所以这可能是我在生产环境中测试时的问题?
我在这里阅读了有关通知类别的信息: iOS8 and iOS7 Push Notification Payload
也许这可以解决它?
这就是我使用 c# 将有效负载发送到苹果的方式
...
try
{
memoryStream = new MemoryStream();
writer = new BinaryWriter(memoryStream);
writer.Write((byte)0); //The command
writer.Write((byte)0); //The first byte of the deviceId length (big-endian first byte)
writer.Write((byte)32); //The deviceId length (big-endian second byte)
//bw.Write(new byte[] { 0, 0, 32 }); Other way of doing this.
writer.Write(this.HexStringToByteArray(token.ToUpper()));
writer.Write((byte)0);
writer.Write((byte)payload.Length);
writer.Write(System.Text.Encoding.UTF8.GetBytes(payload));
writer.Flush();
sslStream.Write(memoryStream.ToArray());
sslStream.Flush();
responseString += " SENT TO: " + token + "<br />";
}
...
private byte[] HexStringToByteArray(string hexString)
{
if (hexString == null)
return null;
if (hexString.Length % 2 == 1)
hexString = '0' + hexString; // Up to you whether to pad the first or last byte
byte[] data = new byte[hexString.Length / 2];
for (int i = 0; i < data.Length; i++)
data[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return data;
}
编辑:我尝试在 PHP 中使用脚本推送通知,但工作正常,所以问题是为什么我的 c# 代码..但在这里找不到问题..
【问题讨论】:
标签: c# ios push-notification