public HttpResponseMessage AddEdit(int userId, int scheduledHours, int scheduledDays, string scheduledNotificationId, bool enabled, DateTime lastRun) { var repo = new UserTrackingScheduleRepository(); var itm = new UserTrackingSchedule(); itm.UserId = userId; itm.ScheduledDays = scheduledDays; itm.ScheduledHours = scheduledHours; itm.ScheduledNotificationId = scheduledNotificationId; itm.Enabled = enabled; itm.LastRun = lastRun; var entity = repo.AddEdit(itm); var json = JsonConvert.SerializeObject(entity); return(new HttpResponseMessage { Content = new StringContent(json, Encoding.UTF8, "application/json") }); }
public async Task <HttpResponseMessage> SchedulePushNotificationAlert(int userId) { var schedulerepo = new UserTrackingScheduleRepository(); var userrepo = new UserRepository(); var user = userrepo.Get(userId); NotificationHubClient hub = NotificationHubClient .CreateClientFromConnectionString("<connection string with full access>", "<hub name>"); var schedule = user.UserTrackingSchedules.Where(_ => _.Enabled).FirstOrDefault(); if (schedule != null && schedule.Enabled) { Notification notification = new AppleNotification("{\"aps\":{\"alert\":\"Happy birthday!\"}}"); var scheduledTime = new DateTimeOffset(DateTime.Today.AddDays(schedule.ScheduledDays - 1), new TimeSpan(schedule.ScheduledHours, 0, 0)); var scheduled = await hub.ScheduleNotificationAsync(notification, scheduledTime); schedule.ScheduledNotificationId = scheduled.ScheduledNotificationId; schedulerepo.AddEdit(schedule); } return(Request.CreateResponse(HttpStatusCode.OK)); }