gpt4 book ai didi

c# - 阿拉伯语的 IOS 推送通知

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:11:00 26 4
gpt4 key购买 nike

请告诉我如何发送非英语的推送通知。

我正在关注 this用于发送推送通知。英文推送通知可以。

但是当我尝试使用非英语语言时,收到的通知为??????????????????????

在谷歌搜索后获得更改有效负载长度的建议。任何人都可以帮助我在以下方法中更改尺寸长度的位置吗?

private static byte[] GeneratePayload(NotificationPayload payload)
{
try
{
//convert Devide 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 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;
}
}

最佳答案

引用this修改了 GeneratePayload 方法,我可以用任何非英语语言发送推送通知。代码如下:

private static byte[] GeneratePayload(NotificationPayload payload)
{
try
{
//convert Devide 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();

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;
}
}

关于c# - 阿拉伯语的 IOS 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34128014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com