OGeek|极客世界-中国程序员成长平台

标题: c# - IOS 阿拉伯语推送通知 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 12:20
标题: c# - IOS 阿拉伯语推送通知

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

我关注 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("ayload 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;
      }
    }



Best Answer-推荐答案


引用 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("ayload 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4