//send data to push notification from firebase server private void RequestFirebaseSendNoti(int status, Lst_DatXe order, Lst_Xe car, Lst_LaiXe driver) { string serverKey = "AAAA62U35Pg:APA91bHm0D9udChK9kBnoZP_5yUDHYOPXy62a4pTa_bTbdpEYY2-Em727VMPElPgm0aXRXjDGFwBltn6ZsO9snHZne6rcR9JhsejNnm0JVqpuEAjZzdcymKXy5bHbGMDYGcRJsc_thFT"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://fcm.googleapis.com/fcm/send"); request.Method = "POST"; request.ContentType = "application/json"; request.Headers.Add(HttpRequestHeader.Authorization, "key=" + serverKey); string notiTitle; if (status == 1) { notiTitle = "Thông báo xe đã nhận:"; } else { notiTitle = "Thông báo hủy xe:"; } string notiMessage = "- Từ: " + order.diem_bat_dau.Trim() + "." + Environment.NewLine + "- Đến: " + order.diem_ket_thuc.Trim() + "." + Environment.NewLine + "- Xe: " + car.loai_xe + Environment.NewLine + "- Biển kiểm soát: " + car.bien_kiem_soat + Environment.NewLine + "- Lái xe: " + driver.ten_lai_xe + Environment.NewLine + "- Số điện thoại: " + driver.so_dien_thoai + Environment.NewLine + "- Khởi hành: " + order.thoi_diem_khoi_hanh; var data = new { to = order.registrationID, notification = new { title = notiTitle, body = notiMessage } }; var serializer = new JavaScriptSerializer(); var json = serializer.Serialize(data); Byte[] byteArray = Encoding.UTF8.GetBytes(json); request.ContentLength = byteArray.Length; using (Stream notiStream = request.GetRequestStream()) { notiStream.Write(byteArray, 0, byteArray.Length); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (Stream dataStreamResponse = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(dataStreamResponse)) { String responseMessage = reader.ReadToEnd(); Console.WriteLine(responseMessage); } } } } }
//save data to db private string saveRequestDataToDb(DataClasses2DataContext db, Dictionary <string, string> carOrder) { string success = "Success"; string fail = "Fail"; carOrder.TryGetValue("originPlace", out string originPlace); carOrder.TryGetValue("originLatitude", out string orignLatitude); carOrder.TryGetValue("originLongitude", out string originLongitude); carOrder.TryGetValue("destinationPlace", out string destinationPlace); carOrder.TryGetValue("destinationLat", out string destinationLatitude); carOrder.TryGetValue("destinationLong", out string destinationLongitude); carOrder.TryGetValue("carType", out string carType); carOrder.TryGetValue("orderTime", out string orderTime); carOrder.TryGetValue("pickupTime", out string pickupTimeString); carOrder.TryGetValue("phoneNumber", out string phoneNumber); carOrder.TryGetValue("refreshed_token", out string registrationID); //decode Lst_DatXe newOrder = new Lst_DatXe(); newOrder.diem_bat_dau = HttpUtility.UrlDecode(originPlace); newOrder.lat_bat_dau = Convert.ToDouble(HttpUtility.UrlDecode(orignLatitude)); newOrder.long_bat_dau = Convert.ToDouble(HttpUtility.UrlDecode(originLongitude)); newOrder.diem_ket_thuc = HttpUtility.UrlDecode(destinationPlace); newOrder.lat_ket_thuc = Convert.ToDouble(HttpUtility.UrlDecode(destinationLatitude)); newOrder.long_ket_thuc = Convert.ToDouble(HttpUtility.UrlDecode(destinationLongitude)); //split: ten xe - so ghe string car_type = HttpUtility.UrlDecode(carType); string[] seatNumber = car_type.Split('-'); int soGhe = Convert.ToInt32(seatNumber[1]); newOrder.so_ghe = soGhe; Table <Lst_LoaiXe> listLoaiXe = db.GetTable <Lst_LoaiXe>(); newOrder.id_loai_xe = (from idloaixe in listLoaiXe where Convert.ToInt32(idloaixe.so_ghe) == soGhe select idloaixe.id_loai_xe).First(); string orderTimeDecoded = HttpUtility.UrlDecode(orderTime); newOrder.thoi_diem_dat_xe = DateTime.ParseExact(orderTimeDecoded, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture); string pickupTimeDecoded = HttpUtility.UrlDecode(pickupTimeString); newOrder.thoi_diem_khoi_hanh = DateTime.ParseExact(pickupTimeDecoded, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture); newOrder.sdt_nguoi_dat = HttpUtility.UrlDecode(phoneNumber); newOrder.status = 0; newOrder.registrationID = HttpUtility.UrlDecode(registrationID); try { db.Lst_DatXes.InsertOnSubmit(newOrder); db.SubmitChanges(); return(success); } catch (Exception e) { return(fail); } }