using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sog; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using LitJson; namespace HttpProxyWorld { public static class FirebaseHttpPushSvc { static string strurl = "https://fcm.googleapis.com/fcm/send"; public static void PushToGlobalTopics(string pushMessage, string paramKey, string paramValue) { HttpClient httpClient = null; try { string strkey = "key=" + HttpProxyWorldServerUtils.GetServerConfig().googlecm_serverkey; JsonData jbuilder = new JsonData(JsonType.Object); jbuilder["to"] = "/topics/global"; jbuilder["time_to_live"] = 86400; jbuilder["delay_while_idle"] = true; JsonData jnotify = new JsonData(JsonType.Object); jnotify["body"] = pushMessage; jnotify["sound"] = "default"; jnotify["icon"] = "icon72"; jnotify["tag"] = "paikang"; jbuilder["notification"] = jnotify; JsonData jdata = new JsonData(JsonType.Object); if (string.IsNullOrEmpty(paramKey) == false && string.IsNullOrEmpty(paramValue) == false) { jdata[paramKey] = paramValue; jdata["pushtype"] = "firebase"; jbuilder["data"] = jdata; } string strcon = jbuilder.ToJson(); //string strcon = "{\"to\" : \"/topics/global\",\"data\":{\"message\": \"" + pushMessage + "\","; //strcon += "}}"; HttpContent content = null; string strutf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(strcon)); content = new StringContent(strutf8); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); httpClient = new HttpClient(); httpClient.MaxResponseContentBufferSize = 256000; httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36"); httpClient.Timeout = TimeSpan.FromSeconds(10); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", strkey); TraceLog.Debug("MessageTaskHandler.OnHttpReqGooglePushInfo strcon {0} strkey {1}", strcon, strkey); HttpResponseMessage response = httpClient.PostAsync(new Uri(strurl), content).Result; if (response != null) { var result = response.Content.ReadAsStringAsync().Result; TraceLog.Debug("MessageTaskHandler.OnHttpReqGooglePushInfo result {0}", result.ToString()); } else { TraceLog.Debug("MessageTaskHandler.OnHttpReqGooglePushInfo response is null"); } } catch (Exception ex) { TraceLog.Debug("MessageTaskHandler.OnHttpReqGooglePushInfo ex {0}", ex); } finally { if (httpClient != null) { httpClient.Dispose(); } } } public static void PushNotificationToIds(string pushMessage, List pushIds, string paramKey, string paramValue) { HttpClient httpClient = null; try { JsonData jbuilder = new JsonData(JsonType.Object); JsonData jids = new JsonData(); foreach (var id in pushIds) { jids.Add(id); } jbuilder["registration_ids"] = jids; jbuilder["time_to_live"] = 86400; jbuilder["delay_while_idle"] = true; JsonData jnotify = new JsonData(JsonType.Object); jnotify["body"] = pushMessage; jnotify["sound"] = "default"; jnotify["icon"] = "icon72"; jnotify["tag"] = "paikang"; jbuilder["notification"] = jnotify; JsonData jdata = new JsonData(JsonType.Object); if (string.IsNullOrEmpty(paramKey) == false && string.IsNullOrEmpty(paramValue) == false) { jdata[paramKey] = paramValue; jdata["pushtype"] = "firebase"; jbuilder["data"] = jdata; } string strcon = jbuilder.ToJson(); /* string registration_ids = Build_registration_ids(pushIds); string strcon = "{" + registration_ids + ",\"data\":{\"message\": \"" + pushMessage + "\",}}"; */ string strkey = "key=" + HttpProxyWorldServerUtils.GetServerConfig().googlecm_serverkey; HttpContent content = null; string strutf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(strcon)); content = new StringContent(strutf8); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); httpClient = new HttpClient(); httpClient.MaxResponseContentBufferSize = 256000; httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36"); httpClient.Timeout = TimeSpan.FromSeconds(10); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", strkey); TraceLog.Debug("MessageTaskHandler.OnHttpReqGooglePushInfo strcon {0} strkey {1}", strcon, strkey); HttpResponseMessage response = httpClient.PostAsync(new Uri(strurl), content).Result; if (response != null) { var result = response.Content.ReadAsStringAsync().Result; TraceLog.Debug("MessageTaskHandler.OnHttpReqGooglePushInfo result {0}", result.ToString()); } else { TraceLog.Debug("MessageTaskHandler.OnHttpReqGooglePushInfo response is null"); } } catch (Exception ex) { TraceLog.Debug("MessageTaskHandler.OnHttpReqGooglePushInfo ex {0}", ex); } finally { if (httpClient != null) { httpClient.Dispose(); } } } } }