pushnotification没有使用asp.net到达ios设备?

时间:2016-05-05 06:39:31

标签: c# ios asp.net push-notification

我正在使用iOSasp.net设备发送推送通知。我已成功发送通知(未收到任何错误)。但设备没有收到任何通知。我已成功发送通知(从服务器端)。

 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();
    }

请帮帮我。谢谢你提前

2 个答案:

答案 0 :(得分:1)

- (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);     
}

你需要:

1)沙盒测试帐户

2)使用沙盒帐户登录设备

3)关于功能中的PushNotification

答案 1 :(得分:0)

这不是确切的答案。可能它会帮助您调试。

在注册远程通知时,请检查didRegisterForRemoteNotificationsWithDeviceTokendidFailToRegisterForRemoteNotificationsWithError。这将帮助您检查设备是否已成功添加或注册时是否有任何错误。