Mukesh

Mukesh

  • NA
  • 228
  • 40k

How To Fix Additional non-parsable characters are at the end

Apr 19 2018 5:14 AM
public void pushMessage(CMSMobApp obj)
{
var senderId = obj.DeviceID;//DeviceID
string message = "You have one new notification";
string title = obj.Title;
string icon = "myicon";
int ModuleId = obj.Module_id;
int Notifid = obj.Notifid;
string deviceId = obj.RegToken;//RegToken
int port = 2195;
String hostname = "gateway.push.apple.com";
String certificatePath = System.Web.Hosting.HostingEnvironment.MapPath("~/MPEDA_Sandbox_Certificates.p12");
X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), "xxxxxx");
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, false);
MemoryStream memoryStream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(memoryStream);
writer.Write((byte)0);
writer.Write((byte)0);
writer.Write((byte)32);
writer.Write(HexStringToByteArray(senderId,message,title,icon,ModuleId,Notifid,deviceId.ToUpper()));
String payload = "{\"aps\":{\"alert\":\"" + "Hi,, This Is a Sample Push Notification For IPhone.." + "\",\"badge\":1,\"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)
{
client.Close();
}
catch (Exception e)
{
client.Close();
}
}
private static byte[] HexStringToByteArray(string senderId,string message,string title,string icon,int ModuleId,int Notifid,string hex)
{
return Enumerable.Range(0, hex.Length)  // getting Exception From this Line
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
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;
}
 
Above i Mention my Code for IOS Push Notification and I getting Error where i Cooment on Line so Please Help me How to Fix this Error