Pavan Reddy
Pavan Reddy

Reputation: 13

pushnotification not getting to ios device using asp.net?

I am sending push notification to iOS device using asp.net. I have successfully sent notification (not getting any error). But device is not getting any notification. I have successfully sent notification(from server side).

 public  void iphonpushnotification(string deviceid)
    {
        string devicetocken =deviceid;//  iphone device token
        int port = 2195;
        String hostname = "gateway.sandbox.push.apple.com";
        //String hostname = "gateway.push.apple.com";

     //   string certificatePath = Server.MapPath("E:\\Certificates.p12");
        string certificatePath = "E:\\pushNotification.p12";

        string certificatePassword = "abc123";

        X509Certificate2 clientCertificate = new X509Certificate2(certificatePath, certificatePassword, 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.Default, false);
        }
        catch (AuthenticationException ex)
        {
            Console.WriteLine("Authentication failed");
            client.Close();
          //  Request.SaveAs(Server.MapPath("Authenticationfailed.txt"), true);
            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)

        byte[] b0 = HexString2Bytes(devicetocken);
        WriteMultiLineByteArray(b0);

        writer.Write(b0);
        String payload;
        string strmsgbody = "";
        int totunreadmsg = 20;
        strmsgbody = "Hey Aashish!";

        Debug.WriteLine("during testing via device!");
       // Request.SaveAs(Server.MapPath("APNSduringdevice.txt"), true);

        payload = "{\"aps\":{\"alert\":\"" + strmsgbody + "\",\"badge\":" + totunreadmsg.ToString() + ",\"sound\":\"mailsent.wav\"},\"acme1\":\"bar\",\"acme2\":42}";

        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();
        Debug.WriteLine("This is being sent...\n\n");
        Debug.WriteLine(array);
        try
        {
            sslStream.Write(array);
            sslStream.Flush();
        }
        catch
        {
            Debug.WriteLine("Write failed buddy!!");
          //  Request.SaveAs(Server.MapPath("Writefailed.txt"), true);
        }

        client.Close();
        Debug.WriteLine("Client closed.");
      //  Request.SaveAs(Server.MapPath("APNSSuccess.txt"), true);
    }
    private byte[] HexString2Bytes(string hexString)
    {
        //check for null
        if (hexString == null) return null;
        //get length
        int len = hexString.Length;
        if (len % 2 == 1) return null;
        int len_half = len / 2;
        //create a byte array
        byte[] bs = new byte[len_half];
        try
        {
            //convert the hexstring to bytes
            for (int i = 0; i != len_half; i++)
            {
                bs[i] = (byte)Int32.Parse(hexString.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
            }
        }
        catch (Exception ex)
        {
            //MessageBox.Show("Exception : " + ex.Message);
        }
        //return the byte array
        return bs;
    }
    // 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;

        Console.WriteLine("Certificate error: {0}", sslPolicyErrors);

        // Do not allow this client to communicate with unauthenticated servers.
        return false;
    }
    public static void WriteMultiLineByteArray(byte[] bytes)
    {
        const int rowSize = 20;
        int iter;

        Console.WriteLine("initial byte array");
        Console.WriteLine("------------------");

        for (iter = 0; iter < bytes.Length - rowSize; iter += rowSize)
        {
            Console.Write(
                BitConverter.ToString(bytes, iter, rowSize));
            Console.WriteLine("-");
        }

        Console.WriteLine(BitConverter.ToString(bytes, iter));
        Console.WriteLine();
    }

please help me.Thanks in advance

Upvotes: 0

Views: 245

Answers (2)

Jabir110
Jabir110

Reputation: 51

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{
        NSLog(@"%@", userInfo);
}

- (void)application:(UIApplication *)application didFailToRegis[enter image description here][1]terForRemoteNotificationsWithError:(NSError
    *)error 
{
        NSLog(@"Did Fail to Register for Remote Notifications");
        NSLog(@"%@, %@", error, error.localizedDescription);     
}

You need:

1) Sandbox Test Account

2) Login in Device Using Sandbox Account

3) On PushNotification in Capabilities

Upvotes: 1

sasi kumar
sasi kumar

Reputation: 753

This is not the exact answer. May be it will help you to debug.

While registering for remote notification, check for didRegisterForRemoteNotificationsWithDeviceToken and didFailToRegisterForRemoteNotificationsWithError. This will help you to check whether the device is successfully added or it has any error in registering.

Upvotes: 0

Related Questions