1

We have been using apple push notification service and we can send push message from gateway.sandbox.push.apple.com ,All I know this address is test, so we have to use real address gateway.push.apple.com, we can not send push message from this address. Could you help us?

Our code is;

public void pushMessage(string deviceID, string Mesaj)
{
    int port = 2195;
    String hostname = "gateway.sandbox.push.apple.com";     // TEST
    //String hostname = "gateway.push.apple.com";           // REAL

    //        @"cert.p12";
    String certificatePath = HttpContext.Current.Server.MapPath("cert.p12");
    //X509Certificate2 clientCertificate = new X509Certificate2(certificatePath, "");


    X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), "", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);



    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 e)
    {
        throw (e);
        client.Close();
        return;
    }
    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\":0,\"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();
}

you can watch the video of the problem

Video link

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
cdsoft
  • 621
  • 2
  • 8
  • 15
  • 3
    What exactly isn't working? Are you getting some sort of an error message? – Roman Dec 26 '12 at 15:22
  • 1
    try this http://stackoverflow.com/questions/2319337/cant-connect-to-production-apple-push-notification-server – Darkface35 Dec 26 '12 at 15:32
  • We can't get any error message. We can send notification from gateway.sandbox.push.apple.com, but we can't send from gateway.push.apple.com – cdsoft Dec 26 '12 at 15:32
  • If you do not receive any error messages how do you know you cannot send to gateway.push.apple.com? – Joe Dec 26 '12 at 15:56
  • @cdsoft Have you tried what @Darkface35 suggests, namely setting `port` to be `30` rather than `2195`? – Nate Chandler Dec 26 '12 at 15:56
  • Message doesn't reach to iphone – cdsoft Dec 26 '12 at 16:03
  • @cdsoft - I am trying to implement the same by looking at ur code. Will you be able to post the whole code. Need function -ValidateServerCertificate. Moreover I am also getting an error that "The parameter is incorrect" while creating clientCertificate object. Tried looking into google but all in vain, appreciate your help. – Punit Jul 11 '13 at 10:50

1 Answers1

2

It sounds like you might be sending information to Apple just fine. This issue might be that your iOS build isn't using the production push notification server.

Make sure your build is either Ad Hoc or Distribution and not Development.

Creating and Downloading a Distribution Provisioning Profile

Joe
  • 2,987
  • 15
  • 24
  • We have been using development cerificate with gateway.sandbox.push.apple.com, and message sending without error. but, when we chang the certificate to distribution maybe message sending successfully but we cannot test it on our iphone. – cdsoft Dec 27 '12 at 09:49
  • So you will need to make sure that both your server cert and your iOS app are using production APNS and not the sandbox. – Joe Dec 27 '12 at 14:10
  • you can watch the video of the problem http://www.youtube.com/watch?v=zVt_g7Q1wV8&feature=youtu.be – cdsoft Dec 27 '12 at 16:04
  • So you CANNOT use the same iOS build like you are doing. You will need to rebuild the app and do it as an AdHoc build for push notifications to work with production APNS: http://stackoverflow.com/questions/5295890/iphone-ad-hoc-build-using-xcode-4 – Joe Dec 27 '12 at 19:14