• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 12:20:22 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

我关注 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap