Mukesh Kumar Tiwari

Mukesh Kumar Tiwari

  • NA
  • 980
  • 252.5k

Send Notification To iPhone Using WCF.

Nov 30 2017 10:07 PM
Hello,
 
I am tryinh to send the notifictions to iPhone. And i ahev made a fucntion for that , there is not any bug in code, fucntion excuted successfully, but notifiction could not send th the iphone device. 
 
Can any one help out from this, please....
 
Below is  my code..
 
tiwari-mike
public void SendMessageiOS(string deviceID, string message)
{
int port = 2195;
String hostname = "gateway.push.apple.com";
//C:\Windows\System32\inetsrv

//load certificate
string certificatePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Loyalty_Prod_Certi.p12");
string certificatePassword = "1234";
// string certificatePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "_DistriCertificates.p12");
// X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), "1234");
X509Certificate2 clientCertificate = new X509Certificate2(certificatePath);
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 ex)
{
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)
//byte[] AppID = Encoding.UTF8.GetBytes(deviceID.ToUpper());
writer.Write(StringToByteArrayFastest(deviceID.ToUpper()));
//writer.Write(AppID);
String payload = "{\"aps\":{\"alert\":\"Message\",\"badge\":0,\"sound\":\"default\"}}";
payload = payload.Replace("Message", message);
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();
}
 

Answers (1)