Domanda

Sto tentando di scrivere un server push per iPhone in C #. Ho il seguente codice:

        // Create a TCP/IP client socket.
        using (TcpClient client = new TcpClient())
        {
            client.Connect("gateway.sandbox.push.apple.com", 2195);
            using (NetworkStream networkStream = client.GetStream())
            {
                Console.WriteLine("Client connected.");

                X509Certificate clientCertificate = new X509Certificate(@"certfile.p12", passwordHere);
                X509CertificateCollection clientCertificateCollection = new X509CertificateCollection(new X509Certificate[1] { clientCertificate });

                // Create an SSL stream that will close the client's stream.
                SslStream sslStream = new SslStream(
                    client.GetStream(),
                    false,
                    new RemoteCertificateValidationCallback(ValidateServerCertificate),
                    null
                    );

                try
                {
                    sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com");
                }
                catch (AuthenticationException e)
                {
                    Console.WriteLine("Exception: {0}", e.Message);
                    if (e.InnerException != null)
                    {
                        Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
                    }
                    Console.WriteLine("Authentication failed - closing the connection.");
                    client.Close();
                    return;
                }
            }

ect ....

Continuo a ricevere solo un'eccezione: " Una chiamata a SSPI non è riuscita, vedere Eccezione interna " Eccezione interna - & Gt; " Il messaggio ricevuto era inatteso o mal formattato. "

Qualcuno ha idea di cosa vada storto qui?

È stato utile?

Soluzione

Capito. Sostituito sslStream.AuthenticateAsClient (& Quot; gateway.sandbox.push.apple.com & Quot;); con sslStream.AuthenticateAsClient (" gateway.sandbox.push.apple.com " ;, clientCertificateCollection, SslProtocols.Default, false); E registrato i certificati sul PC.

Modifica: ecco il codice per creare un payload come richiesto:

    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

Altri suggerimenti

Dal commento di Zenox: utilizzare una versione diversa di AuthenticateAsClient

sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com", clientCertificateCollection, SslProtocols.Default, false);

L'altro modo è usare solo le classi X509Certificate2 e X509CertificateCollection2.

Recentemente ho usato Growl per Windows per inviare messaggi al client Prowl sull'iPhone dal codice .Net . Quindi potresti ottenere la tua funzionalità senza scrivere tu stesso un server push.

Il " Il messaggio ricevuto era imprevisto o mal formattato. " l'errore di solito arriva quando non è stato registrato il certificato p12 in Windows. (Sotto Vista, fai doppio clic sul file p12 e si aprirà la procedura guidata di importazione)

Nel mio caso ho dovuto eliminare tutti i certificati da Windows 8 e quindi reinstallarli per inviare notifiche push al dispositivo Apple.

Non so perché i miei certificati smettono di funzionare, sto cercando il motivo corretto e aggiornerò qui presto.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top