// Firebase Cloud Messaging 보내기. private void sendFCM_lostbike(LOSTBIKE lostBike, string lost_found, PUSHRECORD pushRecord) { string serverKey = "AAAAGb-LKYs:APA91bEuJrpmtMaFicGReVqpinlj-Mzk8qniXLhFkqcCtJVLJ9h7LoPFHK7wDTUlhx7OAhqGgSONUVYvU5BUQey22CegTMWAhzKp3DCiEbjU67fGDaTWt88J2vvuA3WgemufF_OZ4J_6"; try { var result = "-1"; var webAddr = "https://fcm.googleapis.com/fcm/send"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr); httpWebRequest.ContentType = "application/json"; httpWebRequest.Headers.Add("Authorization:key=" + serverKey); httpWebRequest.Method = "POST"; FCMdata fcmData = new FCMdata(); fcmData.to = "/topics/lostbikes"; FCMdataBody fcmBody = new FCMdataBody(); if (lost_found.Equals("L")) { fcmBody.msg_type = "LOST"; fcmBody.title = "분실도움 요청"; fcmBody.msg = lostBike.UserName + " 님이 " + lostBike.BikeNickname + "의 위치를 찾기 위해 분실도움요청을 하셨습니다."; } else { fcmBody.msg_type = "LOSTCANCEL"; fcmBody.title = "분실도움 취소"; fcmBody.msg = lostBike.UserName + " 님이 " + lostBike.BikeNickname + "의 분실신고를 취소하셨습니다."; } fcmBody.data = lostBike.UserName + "_" + lostBike.BikeNickname + "_" + lostBike.DeviceID; fcmData.data = fcmBody; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { string json = Newtonsoft.Json.JsonConvert.SerializeObject(fcmData); //"{\"to\": \"/topics/lostbikes\",\"data\": {\"message\": \"This is a Firebase Cloud Messaging Topic Message!\",}}"; streamWriter.Write(json); streamWriter.Flush(); pushRecord.request = json; } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { result = streamReader.ReadToEnd(); pushRecord.response = result.ToString(); } } catch (Exception ex) { pushRecord.error = ex.Message; // Response.Write(ex.Message); } }
// Firebase Cloud Messaging 보내기. private void sendFCM_lostbike_found(USER theUser, BikeLost bikeLost, PUSHRECORD pushRecord) { string serverKey = "AAAAGb-LKYs:APA91bEuJrpmtMaFicGReVqpinlj-Mzk8qniXLhFkqcCtJVLJ9h7LoPFHK7wDTUlhx7OAhqGgSONUVYvU5BUQey22CegTMWAhzKp3DCiEbjU67fGDaTWt88J2vvuA3WgemufF_OZ4J_6"; try { var result = "-1"; var webAddr = "https://fcm.googleapis.com/fcm/send"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr); httpWebRequest.ContentType = "application/json"; httpWebRequest.Headers.Add("Authorization:key=" + serverKey); httpWebRequest.Method = "POST"; FCMdata fcmData = new FCMdata(); fcmData.to = theUser.FCM_Token; FCMdataBody fcmBody = new FCMdataBody(); fcmBody.msg_type = "FOUND"; fcmBody.title = "자전거 발견"; fcmBody.msg = bikeLost.nickname + "의 " + "최근 위치가 감지되었습니다."; fcmBody.data = bikeLost.nickname + "_" + bikeLost.location_lost + "_" + bikeLost.dt_lost; fcmData.data = fcmBody; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { string json = Newtonsoft.Json.JsonConvert.SerializeObject(fcmData); //"{\"to\": \"/topics/lostbikes\",\"data\": {\"message\": \"This is a Firebase Cloud Messaging Topic Message!\",}}"; streamWriter.Write(json); streamWriter.Flush(); pushRecord.request = json; } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { result = streamReader.ReadToEnd(); pushRecord.response = result.ToString(); } } catch (Exception ex) { pushRecord.error = ex.Message; // Response.Write(ex.Message); } }