public void SendNotification(PublishSurveyViewModel surveyToSend) { try { var surveyToPublish = new X_Survey_Group { SurveyId = surveyToSend.SurveyId, StudyGroupId = surveyToSend.StudyGroupId, SurveyCreatedTime = DateTime.Now.ToString(), FrequencyOfNotifications = surveyToSend.FrequencyOfNotifications, Time1 = surveyToSend.Time1, Time2 = surveyToSend.Time2 }; db.X_Survey_Groups.Add(surveyToPublish); db.SaveChanges(); List <string> deviceIds = new List <string>(); var userIdsInGroup = db.X_User_Groups.Where(u => u.StudyGroupId.Equals(surveyToSend.StudyGroupId)).ToList(); foreach (var userId in userIdsInGroup) { var devicesForOneUser = db.Devices.Where(d => d.UserId.Equals(userId)); foreach (var device in devicesForOneUser) { deviceIds.Add(device.DeviceId); } } var survey = db.Surveys.Find(surveyToSend.SurveyId); String messageToDisplay = ""; if (survey != null) { if (survey.SurveyType == SurveyType.Message) { var question = db.X_Survey_Questions.Where(q => q.SurveyId == survey.Id).FirstOrDefault(); messageToDisplay = question.Question.QuestionText; } else { messageToDisplay = survey.SurveyName + " published!!"; } } SurveyPushNotification notification = new SurveyPushNotification { RegisteredDeviceIds = deviceIds, Data = new PushNotificationData { Message = messageToDisplay, Time = DateTime.Now.ToString() } }; if (deviceIds.Count > 0) { var applicationID = "AIzaSyC0Ian0Yr7JK9tZEi7-dZ3GcO-2dzomG1M"; // applicationID means google Api key var SENDER_ID = "283278634859"; // SENDER_ID is nothing but your ProjectID (from API Console- google code) string serializedNotification = JsonConvert.SerializeObject(notification); WebRequest tRequest; tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send"); tRequest.Method = "post"; tRequest.ContentType = " application/json"; tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID)); tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID)); Byte[] byteArray = Encoding.UTF8.GetBytes(serializedNotification); tRequest.ContentLength = byteArray.Length; Stream dataStream = tRequest.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse tResponse = tRequest.GetResponse(); dataStream = tResponse.GetResponseStream(); StreamReader tReader = new StreamReader(dataStream); String sResponseFromServer = tReader.ReadToEnd(); //Get response from GCM server. Console.WriteLine(sResponseFromServer); tReader.Close(); dataStream.Close(); tResponse.Close(); } } catch (Exception e) { } }
public IHttpActionResult PublishSurvey(PublishSurveyViewModel survey) { var surveyDetails = db.Surveys.Find(survey.SurveyId); if (surveyDetails != null) { if (surveyDetails.SurveyType == SurveyType.Message) { //based on recurrence Add to X_Survey_Group try { switch (survey.FrequencyOfNotifications) { case Frequency.Daily: { String[] times = survey.Time1.Split(':'); String cornExpression = times[1] + " " + times[0] + " * * * "; RecurringJob.AddOrUpdate(survey.SurveyId + "", () => SendNotification(survey), cornExpression, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")); break; } case Frequency.Hourly: { SendNotification(survey); RecurringJob.AddOrUpdate(survey.SurveyId + "", () => SendNotification(survey), Cron.Hourly, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")); break; } case Frequency.TwiceDaily: { String[] times = survey.Time1.Split(':'); String cornExpression = times[1] + " " + times[0] + " * * *"; String[] times2 = survey.Time2.Split(':'); String cornExpression2 = times2[1] + " " + times2[0] + " * * *"; RecurringJob.AddOrUpdate(survey.SurveyId + "First", () => SendNotification(survey), cornExpression, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")); RecurringJob.AddOrUpdate(survey.SurveyId + "Second", () => SendNotification(survey), cornExpression2, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")); break; } } } catch (DbUpdateException) { if (SurveyExists(survey.SurveyId)) { return(Conflict()); } else { throw; } } } else { //add to X_Survey_Group and send right now SendNotification(survey); } } else { return(NotFound()); } return(null); }