在Web和App之间使用FCM推送通知

时间:2018-12-21 04:37:17

标签: c# firebase android-studio firebase-cloud-messaging xcode8

我试图实现从C#.Net(MVC)Web到React Native(Android和iPhone)之间的推送通知。我的代码中没有显示错误,但App中没有显示通知。

我已经尝试作为堆栈溢出中的参考

try
  {
 string server_api_key =  ConfigurationManager.AppSettings["SERVER_API_KEY"];
 string sender_id = ConfigurationManager.AppSettings["SENDER_ID"];
 WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
 tRequest.Method = "post";
 tRequest.ContentType = "application/json";
 tRequest.Headers.Add($"Authorization: key={server_api_key}");
 tRequest.Headers.Add($"Sender: id={sender_id}");
 var data = new
 {
 to = "AAAAaQ5neZA:................................XXXX",
 priority = "high",
 data = new
 {
   message = response,
   name = loginUser.EmployeeName,
   userId = uId,
   status = true
   }
 };
  var serializer = new JavaScriptSerializer();
  var json = serializer.Serialize(data);
  Byte[] byteArray = Encoding.UTF8.GetBytes(json);

  tRequest.ContentLength = byteArray.Length;
  Stream dataStream = tRequest.GetRequestStream();
  dataStream.Write(byteArray, 0, byteArray.Length);               

  WebResponse tresponse = tRequest.GetResponse();
  dataStream = tresponse.GetResponseStream();
  StreamReader tReader = new StreamReader(dataStream);
  string sResponseFromServer = tReader.ReadToEnd();

  tReader.Close();
  dataStream.Close();
  tresponse.Close();
  return sResponseFromServer;
}
catch (Exception)
   {
     throw;
   }

0 个答案:

没有答案