4
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text;
using System.Security.Authentication;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using Newtonsoft.Json.Linq;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnPush_Click(object sender, EventArgs e)
        {
            pushMessage(txtDeviceID.Text.Trim(), txtPayload.Text.Trim());
        }


        public void pushMessage(string deviceID, string Mesaj)
        {

            int port = 2195;
            String hostname = "gateway.push.apple.com";

            String certificatePath = HttpContext.Current.Server.MapPath("new_dev_cert.p12");
            X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), "taxmann");
            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.Ssl3, false);

                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(HexStringToByteArray(deviceID.ToUpper()));
                String payload = "{\"aps\":{\"alert\":\"" + Mesaj + "\",\"badge\":1,\"sound\":\"default\"}}";
                writer.Write((byte)0);
                writer.Write((byte)payload.Length);
                byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload);
                writer.Write(b1);
                writer.Flush();
                byte[] array = memoryStream.ToArray();
                sslStream.Write(array);
                sslStream.Flush();
                client.Close();
                lblResponse.Text = "Sucess..";
            }
            catch (System.Security.Authentication.AuthenticationException ex)
            {
                client.Close();
                lblResponse.Text = ex.Message;
            }   
            catch (Exception e)
            {
                client.Close();
                lblResponse.Text = e.Message;
            }
        }

        // The following method is invoked by the RemoteCertificateValidationDelegate. 
        public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            if (sslPolicyErrors == SslPolicyErrors.None)
                return true;
            else // Do not allow this client to communicate with unauthenticated servers. 
                return false;
        }

        private static byte[] HexStringToByteArray(String DeviceID)
        {
            //convert Devide token to HEX value.
            byte[] deviceToken = new byte[DeviceID.Length / 2];
            for (int i = 0; i < deviceToken.Length; i++)
                deviceToken[i] = byte.Parse(DeviceID.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);

            return deviceToken;
        }
    }
}

Above is my source code of the Back End for sending Notification. My code debugs successfully and getting message. But I am not able to Get Notification on iPhone.

I have followed this http://blog.serverdensity.com/how-to-build-an-apple-push-notification-provider-server-tutorial/. I don't know where is the problem is. Can you please help me out on this issue.

  • Just to make sure you've added push notifications to the app signing certificate? – shoughton123 Mar 08 '13 at 10:49
  • i have added already and code is able to read that certificate ...code is able to read all line when i debug get response – user2148026 Mar 08 '13 at 10:52
  • I have tried Much But not able to do please help me – user2148026 Mar 08 '13 at 10:53
  • I mean on the developer certificate not the server certificate as in the one you need for running the app on devices. Also on your ios app have you registered to server notifications. Is it possible that your ios app code is wrong? You are aware that notifications work through an iPhone app right? You can't just send notifications to any random device. – shoughton123 Mar 08 '13 at 10:59
  • I have generated That certificate from Mac OS from My active account .. U please check our our code is it fine coz am able to run it But when i Push message with Particular Iphone device id then NO response he Got how to generate server certificate and how generate developer certificate – user2148026 Mar 08 '13 at 11:04
  • Well your server code looks fine, is it possible your ios app code is wrong? – shoughton123 Mar 08 '13 at 11:05
  • can U provide me IOS code from which i can check where i am doing wrong ? – user2148026 Mar 08 '13 at 11:08
  • Read the tutorial I have posted as an answer I've used it many times and has always worked for me :) – shoughton123 Mar 08 '13 at 11:10

1 Answers1

0

Go through this tutorial it explains everything you need from start to finish and works perfectly. It explains how to set up everything server side and on the ios device as well as sorting out all the certificates in the developer portal.

http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12

Hope this helps

Sam

{
    "aps":
    {
        "alert":
        {
            "body": "Hello, world!"
        },
        "badge": 2
    }
}
shoughton123
  • 4,255
  • 3
  • 21
  • 23