c# - IOS 阿拉伯语推送通知
<p><p>请告诉我如何以非英语语言发送推送通知。</p>
<p>我关注 <a href="http://www.c-sharpcorner.com/UploadFile/pandeypradip/apple-push-notification-using-Asp-Net/" rel="noreferrer noopener nofollow">this</a>用于发送推送通知。英文推送通知没问题。</p>
<p>但是当我尝试使用非英语语言获取通知时,<code>???????????????????</code>。</p>
<p>在谷歌搜索后获得更改有效负载长度的建议。任何人都可以帮助我在以下方法中更改尺寸长度吗?</p>
<pre><code>private static byte[] GeneratePayload(NotificationPayload payload)
{
try
{
//convert Devide token to HEX value.
byte[] deviceToken = new byte;
for (int i = 0; i < deviceToken.Length; i++)
deviceToken = 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 ExpiryDate to Payload
int epoch = (int) (DateTime.UtcNow.AddMinutes(300) - new DateTime(1970, 1, 1)).TotalSeconds;
byte[] timeStamp = BitConverter.GetBytes(epoch);
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();
Logger.Info("Payload generated for " + payload.DeviceToken + " : " + apnMessage);
byte[] apnMessageLength = BitConverter.GetBytes((Int16) apnMessage.Length);
Array.Reverse(apnMessageLength);
// message length
memoryStream.Write(apnMessageLength, 0, 2);
// Write the message
memoryStream.Write(Encoding.ASCII.GetBytes(apnMessage), 0, apnMessage.Length);
return memoryStream.ToArray();
}
catch (Exception ex)
{
Logger.Error("Unable to generate payload - " + ex.Message);
return null;
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>引用 <a href="https://stackoverflow.com/questions/1020762/does-anyone-know-how-to-write-an-apple-push-notification-provider-in-c" rel="noreferrer noopener nofollow">this</a>修改了 GeneratePayload 方法,我能够以任何非英语语言发送推送通知。
代码如下:</p>
<pre><code>private static byte[] GeneratePayload(NotificationPayload payload)
{
try
{
//convert Devide token to HEX value.
byte[] deviceToken = new byte;
for (int i = 0; i < deviceToken.Length; i++)
deviceToken = byte.Parse(payload.DeviceToken.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
var memoryStream = new MemoryStream();
BinaryWriter 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)
writer.Write(deviceToken);
writer.Write((byte)0); //First byte of payload length; (big-endian first byte)
byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload.ToJson());
writer.Write((byte)b1.Length);
writer.Write(b1);
writer.Flush();
byte[] array = memoryStream.ToArray();
// 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 ExpiryDate to Payload
int epoch = (int) (DateTime.UtcNow.AddMinutes(300) - new DateTime(1970, 1, 1)).TotalSeconds;
byte[] timeStamp = BitConverter.GetBytes(epoch);
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();
Logger.Info("Payload generated for " + payload.DeviceToken + " : " + apnMessage);
byte[] apnMessageLength = BitConverter.GetBytes((Int16)apnMessage.Length);
Array.Reverse(apnMessageLength);
// message length
memoryStream.Write(apnMessageLength, 0, 2);
// Write the message
memoryStream.Write(Encoding.ASCII.GetBytes(apnMessage), 0, apnMessage.Length);
return array;
}
catch (Exception ex)
{
Logger.Error("Unable to generate payload - " + ex.Message);
return null;
}
}
</code></pre></p>
<p style="font-size: 20px;">关于c# - IOS 阿拉伯语推送通知,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/34128014/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/34128014/
</a>
</p>
页:
[1]