private PushDetails Push_Apple(PushSentData message) { return(new PushDetails() { Status = ResponseStatus.Failure }); }
public PushDetails Push(PushDeviceType pushDeviceType, PushSentData message) { switch (pushDeviceType)//Todo: move to Factory { case PushDeviceType.Google: return(Push_Google(message)); case PushDeviceType.Apple: return(Push_Apple(message)); } return(new PushDetails() { Status = ResponseStatus.Failure, PushRequest = new PushRequest() { PostedData = "NA" }, PushResponse = "NA", }); }
public PushDetails Push_Google(PushSentData message) { //TODO: the part of dividing to 1000s should be here. not in the previous function. WebRequest request; string postData = null; try { message.RegistrationIds = message.RegistrationIds.Where(x => !string.IsNullOrEmpty(x)); postData = JsonConvert.SerializeObject(message, JsonSerializerSettings); if (!message.RegistrationIds.Any()) { return(new PushDetails() { Status = ResponseStatus.Failure, PushRequest = new PushRequest() { PostedData = postData, Headers = null }, PushResponse = string.Empty }); } request = WebRequest.Create(PushGoogleUri); request.Method = "POST"; request.Headers.Add(String.Format("Authorization: key={0}", PushGoogleAppId)); request.Headers.Add(String.Format("Sender: id={0}", PushGoogleSenderId)); #region Request - As an object request.ContentType = "application/json;"; using (var requestStream = request.GetRequestStream()) { using (var sw = new StreamWriter(requestStream)) { sw.Write(postData); } } } catch (Exception e) { return(new PushDetails() { Status = ResponseStatus.Failure, PushRequest = new PushRequest() { PostedData = postData, Headers = null }, PushResponse = e.Message }); } #endregion try { #region Response var response = request.GetResponse(); string responseStr; using (var responseStream = response.GetResponseStream()) { using (var rs = new StreamReader(responseStream)) { responseStr = rs.ReadToEnd(); } } var responseFromGoogle = JsonConvert.DeserializeObject <ResponseFromGoogle>(responseStr, JsonSerializerSettings); #endregion return(new PushDetails() { Status = ResponseStatus.Success, PushRequest = new PushRequest() { PostedData = postData, Headers = request.Headers }, PushResponse = responseStr, RawResponse = responseFromGoogle }); } catch (Exception e) { return(new PushDetails() { Status = ResponseStatus.Failure, PushRequest = new PushRequest() { PostedData = postData, Headers = request.Headers }, PushResponse = e.Message }); } }
public static PushDetails Push_Google(PushSentData message) { JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; string PushGoogleUri = "https://android.googleapis.com/gcm/send"; string PushGoogleAppId = "AIzaSyAHwgci8LzCMMRdJRiiklA0_IGJ5Y_1QQU"; string PushGoogleSenderId = "664789495415"; WebRequest request; string postData = null; try { message.RegistrationIds = message.RegistrationIds.Where(x => !string.IsNullOrEmpty(x)); postData = JsonConvert.SerializeObject(message, JsonSerializerSettings); if (!message.RegistrationIds.Any()) { return(new PushDetails() { Status = ResponseStatus.Failure, PushRequest = new PushRequest() { PostedData = postData, Headers = null }, PushResponse = string.Empty }); } request = WebRequest.Create(PushGoogleUri); request.Method = "POST"; request.Headers.Add(String.Format("Authorization: key={0}", PushGoogleAppId)); request.Headers.Add(String.Format("Sender: id={0}", PushGoogleSenderId)); #region Request - As an object request.ContentType = "application/json;"; using (var requestStream = request.GetRequestStream()) { using (var sw = new StreamWriter(requestStream)) { sw.Write(postData); } } } catch (Exception e) { return(new PushDetails() { Status = ResponseStatus.Failure, PushRequest = new PushRequest() { PostedData = postData, Headers = null }, PushResponse = e.Message }); } #endregion try { #region Response var response = request.GetResponse(); string responseStr; using (var responseStream = response.GetResponseStream()) { using (var rs = new StreamReader(responseStream)) { responseStr = rs.ReadToEnd(); } } var responseFromGoogle = JsonConvert.DeserializeObject <ResponseFromGoogle>(responseStr, JsonSerializerSettings); #endregion return(new PushDetails() { Status = ResponseStatus.Success, PushRequest = new PushRequest() { PostedData = postData, Headers = request.Headers }, PushResponse = responseStr, RawResponse = responseFromGoogle }); } catch (Exception e) { return(new PushDetails() { Status = ResponseStatus.Failure, PushRequest = new PushRequest() { PostedData = postData, Headers = request.Headers }, PushResponse = e.Message }); } }