Ragini Bhondve

Ragini Bhondve

  • NA
  • 34
  • 2.2k

I m creating api in C# mvc for Sending notification to user mobile.

Aug 28 2020 6:03 AM
I m Getting Error Pls give solution ..My code is below..
 
public IHttpActionResult SendMessage()
{
Basic_Response response = new Basic_Response();
try
{
var data = new
{
to = "fxf-kjhSSUy41_XsJxAcvg:APA91bHH-V1nn6hIM6NKRizlA3Edza2B9eXDfUrQJOQEYpsVMPMJKcwZEBefSmlel3cmW6rs54yY9etQ2rRxTWjoJX_ivpzofHZhzk4Q45Yqox5WVG3gbUHx-fBLT_GUM0XLlbS9Abyz", //token id
data = new
{
message = "hello",
name = "ABC",
UserID = "3015",
status = true
}
};
SendNotification(data);
response.StatusCode = 200;
response.Message = "notification send Successfully";
}
catch (Exception ex)
{
response.Message = null;
response.StatusCode = 404;
}
return Ok(response);
}
public void SendNotification(object data) //conver data into byte array
{
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(data);
Byte[] bytearr = Encoding.UTF8.GetBytes(json);
SendNotification(bytearr);
}
public void SendNotification(Byte[] bytearr)
{
try
{
string server_api_key = ConfigurationManager.AppSettings["SERVER_API_KEY"];
string sender_id = ConfigurationManager.AppSettings["SERVER_ID"];
// WebRequest tRequest = WebRequest.Create("https://zukuz-a363a.firebaseio.com");
WebRequest tRequest = WebRequest.Create("......");
tRequest.UseDefaultCredentials = true;
tRequest.Method = "post";
tRequest.ContentType = "application/json";
tRequest.Headers.Add($"Authorization: key={server_api_key}");
tRequest.Headers.Add($"Sender: id={sender_id}");
tRequest.ContentLength = bytearr.Length;
Stream datastream = tRequest.GetRequestStream();
datastream.Write(bytearr, 0, bytearr.Length);
// datastream.Close();
tRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
//tRequest.UseDefaultCredentials = true;
WebResponse tresponse = tRequest.GetResponse();   //Error : The remote server returned an error: (403) Forbidden
datastream = tresponse.GetResponseStream();
StreamReader tReader = new StreamReader(datastream);
string sResponseFromServer = tReader.ReadToEnd();
tReader.Close();
datastream.Close();
tresponse.Close();
}
catch (Exception ex)
{
throw ex;
}
}

Answers (1)