簡體   English   中英

IOS的推送通知不起作用

[英]Push Notification for IOS not working

我們使用“僅私鑰”選項創建了證書.p12。 我們嘗試通過MAC進行推送通知,並且工作正常。 我用下面的代碼沒有給出任何錯誤,但iPhone沒有得到任何通知。 在這個Helper中是我的班級,將日志寫入文件。

public void PushNotificationIOS(string message, string registrationKey, int type)
    {
        string deviceID = registrationKey; 
        int port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["IosPort"]); //2195;
        string hostname = System.Configuration.ConfigurationManager.AppSettings["HostName"];//"gateway.sandbox.push.apple.com";
        string certPath = string.Empty;
        certPath = System.Configuration.ConfigurationManager.AppSettings["CertificatePath"] + System.Configuration.ConfigurationManager.AppSettings["CertificateName"];//"~/Content/PushCertificateNew.p12";            
        string certificatePath = System.Web.Hosting.HostingEnvironment.MapPath(certPath);
        string certPassword = System.Configuration.ConfigurationManager.AppSettings["CertificatePassword"];
        TcpClient client = new TcpClient(hostname, port);
        try
        {
            X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), certPassword);
            X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);
            SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);

            try
            {
                sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, false);
            }
            catch (Exception ex)
            {
                Helper.WriteLog("sslStream.AuthenticateAsClient : TLS  " + ex.Message);
            }

            MemoryStream memoryStream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(memoryStream);
            writer.Write((byte)0);
            writer.Write((byte)0);
            writer.Write((byte)32);
            writer.Write(StringToByteArray(deviceID.ToUpper()));
            String payload = "{\"aps\":{\"alert\":\"" + message + "\",\"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();
        }
        catch (System.Security.Authentication.AuthenticationException ex)
        {
            Helper.WriteLog("PushNotificationIOS catch  I :" + ex.Message);
            client.Close();
        }
        catch (Exception e)
        {
            Helper.WriteLog("PushNotificationIOS catch  II :" + e.Message);
            client.Close();
        }

    }

誰能告訴我們如何追蹤問題?

我認為問題出在KeyChain的.p12創建過程中。

您可以選擇證書,然后打開箭頭以選擇私鑰,然后從“鑰匙串訪問”中將它們一起導出為.p12文件。

在此處輸入圖片說明

由此您可以在終端中使用以下命令生成.pem文件

cd
cd Desktop
openssl pkcs12 -in pushcert.p12 -out pushcert.pem -nodes -clcerts

您可以參考詳細信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM