How to write an Apple Push Notification Provider in C#?

后端 未结 5 1234
感动是毒
感动是毒 2020-12-02 06:31

Apple really had bad documentation about how the provider connects and communicates to their service (at the time of writing - 2009). I am confused about the protocol. How i

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-02 07:09

    I hope this is relevant (slightly), but I have just successfully created one for Java, so conceptually quite similar to C# (except perhaps the SSL stuff, but that shouldn't be too hard to modify. Below is a sample message payload and crypto setup:

        int port = 2195;
        String hostname = "gateway.sandbox.push.apple.com";
    
    
        char []passwKey = "".toCharArray();
        KeyStore ts = KeyStore.getInstance("PKCS12");
        ts.load(new FileInputStream("/path/to/apn_keystore/cert.p12"), passwKey);
    
        KeyManagerFactory tmf = KeyManagerFactory.getInstance("SunX509");
        tmf.init(ts,passwKey);
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(tmf.getKeyManagers(), null, null);
        SSLSocketFactory factory =sslContext.getSocketFactory();
        SSLSocket socket = (SSLSocket) factory.createSocket(hostname,port); // Create the ServerSocket
        String[] suites = socket.getSupportedCipherSuites();
        socket.setEnabledCipherSuites(suites);
        //start handshake
    
        socket.startHandshake();
    
    
        // Create streams to securely send and receive data to the server
        InputStream in = socket.getInputStream();
        OutputStream out = socket.getOutputStream();
    
    
    
        // Read from in and write to out...
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos.write(0); //The command
        System.out.println("First byte Current size: " + baos.size());
    
        baos.write(0); //The first byte of the deviceId length    
        baos.write(32); //The deviceId length
    
        System.out.println("Second byte Current size: " + baos.size());
    
        String deviceId = "

    }

    Once again, not C#, but at least closer than the poor ObjC sample that Apple provides.

提交回复
热议问题