我正在尝试使用C#发送Apple推送通知。我已经设置了代码并且一切正常,除了Apple的响应是说无效的有效载荷大小。我查看了代码,但我没有看到无效大小的来源。下面是我生成要发送的有效负载的代码。我正在使用Moon APNS发送类库发送。任何人都可以提供的任何帮助将非常感激。
private static byte[] GeneratePayload(NotifcationPayload payload)
{
try
{
//Convert Device token to HEX value
byte[] deviceToken = new byte[payload.DeviceToken.Length /2];
for (int i= 0; i < deviceToken.Length; i++)
deviceToken[i] = byte.Parse(payload.DeviceToken.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
var memoryStream = new MemoryStream();
// Command
memoryStream.WriteByte(1); // Changed command Type
// Adding ID to Payload
memoryStream.Write(Encoding.ASCII.GetBytes(payload.PayloadId.ToString()), 0, payload.PayloadId.ToString().Length);
//Adding Expiration Date to Payload
int expire = (int) (DateTime.UtcNow.AddMinutes(300) - new DateTime(1970, 1, 1)).TotalSeconds;
byte[] timeStamp = BitConverter.GetBytes(expire);
memoryStream.Write(timeStamp, 0 , timeStamp.Length);
byte[] tokenLength = BitConverter.GetBytes((Int16) 32);
Array.Reverse(tokenLength);
//Device Token Length
memoryStream.Write(tokenLength, 0 ,2);
//Token
memoryStream.Write(deviceToken, 0, 32);
//String length
string apnMessage = payload.ToJson();
LogHelper.Info("Payload generated for " + payload.DeviceToken + " : " + apnMessage);
byte[] apnMessageLength = BitConverter.GetBytes((Int16) apnMessage.Length);
Array.Reverse(apnMessageLength, 0, 2);
LogHelper.Info("PushNotification::GeneratePayload : This is the size of the payload " + apnMessageLength);
//Message Length
memoryStream.Write(apnMessageLength, 0, 2);
//Write the Message
memoryStream.Write(Encoding.ASCII.GetBytes(apnMessage), 0, apnMessage.Length);
LogHelper.Info("Writethe message " + memoryStream);
return memoryStream.ToArray();
}
catch (Exception ex)
{
LogHelper.Error("Unable to generate payload - " + ex.Message);
return null;
}
}