Click here to Skip to main content
15,878,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

How we can Send Push Notification for Android and iPhone in ASP.NET Web Service.

What are the steps I will have to follow and all settings ?

Thanks
Posted

For Android :

public void Notify(string regId)
{
var applicationID = "Application Id generated from google account comes here";
var result = "";
var SENDER_ID = "12 digit id you will get from google account";
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
httpWebRequest.Headers.Add(string.Format("Sender: key={0}", SENDER_ID));
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"registration_ids\":[\"" + regId + "\"]," + "\"data\": { \"message\" : \"Your Message Text Will Come here..\"}}";
Console.WriteLine(json);
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
}
}
--------------------------------------------------------------------------------------
For iPhone :

public void pushMessage(string deviceID)
{
int port = 2195;
String hostname = "gateway.sandbox.push.apple.com";

String certificatePath = HttpContext.Current.Server.MapPath("CertificateNameCommeshere.p12");
X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), "PasswordofCertificateComeshere");
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.Ssl3, 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(deviceID.ToUpper()));
String payload = "{\"aps\":{\"alert\":\"" + "Your Message Text Will Come here.." + "\",\"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();
}
}

I got these solutions..
 
Share this answer
 
v2
Getting below error while executing above method

An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code

Additional information: The remote server returned an error: (401) Unauthorized.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900