Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Sending notification to IOS device using C# code

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 375
    Comment on it

    In one of my current project, I had to develop and integrate the IOS notification functionality in my server side C# code. I did some RnD for this and found below mentioned code to serve this purpose.

    public void SendNotification(string deviceId, string message, int badgeCount)
            {  
    int port = 2195;
                        String hostname = "gateway.sandbox.push.apple.com";
    
                        string certificatePath = gsm.NotificationIOSCertificatePath;                    string certificatePassword = gsm.NotificationIOSCertificatePassword;
                        X509Certificate2 clientCertificate = new X509Certificate2(certificatePath, certificatePassword, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet);
                        X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);
    
                        TcpClient client = new TcpClient(hostname, port);
                        SslStream sslStream = new SslStream(
                                client.GetStream(),
                                false,
                                new RemoteCertificateValidationCallback(ValidateServerCertificate),
                                null
                        );
    
                        try
                        {
                            sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, true);
                        }
                        catch (Exception ex)
                        {
                            logger.ErrorFormat(ex.Message);
                            client.Close();
                            return;
                        }
    
                        // Encode a test message into a byte array.
                        MemoryStream 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(HexStringToBytes(deviceId.ToUpper()));
    
                        String payload = "{\"aps\":{\"alert\":\"" + message + "\",\"sound\":\"default\",\"badge\":" + badgeCount + "}}";
    
                        writer.Write((byte)0); //First byte of payload length; (big-endian first byte)
                        writer.Write((byte)payload.Length); //payload length (big-endian second byte)
    
                        byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload);
                        writer.Write(b1);
                        writer.Flush();
    
                        byte[] array = memoryStream.ToArray();
                        sslStream.Write(array);
                        sslStream.Flush();
    
                        // Close the client connection.
                        client.Close();
    }
    

    In above code, few things are prerequisite which are as follows.

    1. deviceId.
    2. message.
    3. badgeCount.
    4. X509Certificate2Collection and its password.

    DeviceId is needed to identify which device the notification has to be sent to.
    Message contains the notification text.
    badgeCount is useful for showing badgecount on the app icon in IOS device.
    X509Certificate2Collection certificate is validated with the certificate of App in IOS device.
    Notifications are sent through "gateway.sandbox.push.apple.com" gateway with port number 2195

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: