C# iPhone push server?

前端 未结 6 986
一生所求
一生所求 2020-12-22 18:16

Im attempting to write a push server for the iPhone in C#. I have the following code:

        // Create a TCP/IP client socket.
        using (TcpClient clie         


        
6条回答
  •  萌比男神i
    2020-12-22 18:40

    Figured it out. Replaced sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com"); with sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com", clientCertificateCollection, SslProtocols.Default, false); And registered the certificates on the PC.

    Edit: Here is the code for creating a payload as requested:

        private static byte[] GeneratePayload(byte [] deviceToken, string message, string sound)
        {
            MemoryStream memoryStream = new MemoryStream();
    
            // Command
            memoryStream.WriteByte(0);
    
            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 = string.Format ( "{{\"aps\":{{\"alert\":{{\"body\":\"{0}\",\"action-loc-key\":null}},\"sound\":\"{1}\"}}}}",
                message,
                sound);
    
            byte [] apnMessageLength = BitConverter.GetBytes((Int16)apnMessage.Length);
            Array.Reverse ( apnMessageLength );
            // message length
            memoryStream.Write(apnMessageLength, 0, 2);
    
            // Write the message
            memoryStream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(apnMessage), 0, apnMessage.Length);
    
            return memoryStream.ToArray();
        } // End of GeneratePayload
    

提交回复
热议问题