|
| 1 | +using System; |
| 2 | +using SimpleToDoService.Entities; |
| 3 | +using SimpleToDoService.Repository; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using Newtonsoft.Json.Linq; |
| 7 | +using System.Net; |
| 8 | +using System.IO; |
| 9 | + |
| 10 | +namespace SimpleToDoService.Common |
| 11 | +{ |
| 12 | + public class PushNotificationScheduler |
| 13 | + { |
| 14 | + private readonly IToDoRepository repository; |
| 15 | + |
| 16 | + public PushNotificationScheduler(IToDoRepository repository) |
| 17 | + { |
| 18 | + this.repository = repository; |
| 19 | + } |
| 20 | + |
| 21 | + bool HasEqualDueDatest(Task task, PushNotification notification) |
| 22 | + { |
| 23 | + var taskDate = task.CheckedTargetDate(); |
| 24 | + var notificationDate = notification?.DueDate; |
| 25 | + |
| 26 | + if (taskDate.HasValue && notificationDate.HasValue) |
| 27 | + { |
| 28 | + return taskDate.Value.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss.fffzz") == |
| 29 | + notificationDate.Value.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss.fffzz"); |
| 30 | + } |
| 31 | + else |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + public async System.Threading.Tasks.Task SchedulePushNotifications(Task task) |
| 36 | + { |
| 37 | + var currentNotification = task.PushNotifications.FirstOrDefault(); |
| 38 | + |
| 39 | + if (!task.Completed && HasEqualDueDatest(task, currentNotification)) |
| 40 | + return; |
| 41 | + |
| 42 | + await DeletePushNotification(currentNotification); |
| 43 | + |
| 44 | + if (!task.Completed) |
| 45 | + await CreatePushNotification(task); |
| 46 | + } |
| 47 | + |
| 48 | + async System.Threading.Tasks.Task DeletePushNotification(PushNotification notification) |
| 49 | + { |
| 50 | + if (notification == null) |
| 51 | + return; |
| 52 | + |
| 53 | + var url = String.Format("https://onesignal.com/api/v1/notifications/{0}?app_id={1}", |
| 54 | + notification.ServiceId.ToString(), Environment.GetEnvironmentVariable("ONE_SIGNAL_APP_ID")); |
| 55 | + var request = WebRequest.Create(url) as HttpWebRequest; |
| 56 | + |
| 57 | + request.ContentType = "application/json; charset=utf-8"; |
| 58 | + request.Method = "DELETE"; |
| 59 | + request.Headers["authorization"] = String.Format("Basic {0}", Environment.GetEnvironmentVariable("ONE_SIGNAL_KEY")); |
| 60 | + |
| 61 | + try |
| 62 | + { |
| 63 | + using (var writer = await request.GetRequestStreamAsync()) |
| 64 | + { |
| 65 | + writer.Write(new Byte[0], 0, 0); |
| 66 | + } |
| 67 | + await request.GetResponseAsync(); |
| 68 | + } |
| 69 | +#if DEBUG |
| 70 | + catch (WebException ex) |
| 71 | + { |
| 72 | + System.Diagnostics.Debug.WriteLine(ex.Message); |
| 73 | + System.Diagnostics.Debug.WriteLine(new StreamReader(ex.Response.GetResponseStream()).ReadToEnd()); |
| 74 | + } |
| 75 | +#else |
| 76 | + catch { } |
| 77 | +#endif |
| 78 | + |
| 79 | + repository.DeletePushNotification(notification); |
| 80 | + } |
| 81 | + |
| 82 | + async System.Threading.Tasks.Task CreatePushNotification(Task task) |
| 83 | + { |
| 84 | + var notificationDate = task.CheckedTargetDate(); |
| 85 | + if (!notificationDate.HasValue) |
| 86 | + return; |
| 87 | + |
| 88 | + var request = WebRequest.Create("https://onesignal.com/api/v1/notifications") as HttpWebRequest; |
| 89 | + |
| 90 | + request.Method = "POST"; |
| 91 | + request.ContentType = "application/json; charset=utf-8"; |
| 92 | + |
| 93 | + request.Headers["authorization"] = String.Format("Basic {0}", Environment.GetEnvironmentVariable("ONE_SIGNAL_KEY")); |
| 94 | + |
| 95 | + var sendJson = JObject.FromObject(new |
| 96 | + { |
| 97 | + app_id = Environment.GetEnvironmentVariable("ONE_SIGNAL_APP_ID"), |
| 98 | + contents = new { en = task.Description }, |
| 99 | + filters = new[] { new { field = "tag", key = "user_id", relation = "=", value = repository.User(task.UserUuid).FirebaseId } }, |
| 100 | + send_after = notificationDate.Value.ToString("yyyy-MM-dd HH:mm:ss 'GMT'zzzz") |
| 101 | + }); |
| 102 | + |
| 103 | + var byteArray = Encoding.UTF8.GetBytes(sendJson.ToString()); |
| 104 | + |
| 105 | + try |
| 106 | + { |
| 107 | + using (var writer = await request.GetRequestStreamAsync()) |
| 108 | + { |
| 109 | + writer.Write(byteArray, 0, byteArray.Length); |
| 110 | + } |
| 111 | + |
| 112 | + using (var response = await request.GetResponseAsync()) |
| 113 | + using (var reader = new StreamReader(response.GetResponseStream())) |
| 114 | + { |
| 115 | + var json = JObject.Parse(reader.ReadToEnd()); |
| 116 | + var notificationId = json["id"].ToString(); |
| 117 | + if (notificationId != null && notificationId.Length > 0) |
| 118 | + { |
| 119 | + var notification = new PushNotification() |
| 120 | + { |
| 121 | + ServiceId = new Guid(notificationId), |
| 122 | + TaskUuid = task.Uuid, |
| 123 | + UserUuid = task.UserUuid, |
| 124 | + DueDate = notificationDate.Value |
| 125 | + }; |
| 126 | + |
| 127 | + repository.CreatePushNotification(notification); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +#if DEBUG |
| 132 | + catch (WebException ex) |
| 133 | + { |
| 134 | + System.Diagnostics.Debug.WriteLine(ex.Message); |
| 135 | + System.Diagnostics.Debug.WriteLine(new StreamReader(ex.Response.GetResponseStream()).ReadToEnd()); |
| 136 | + } |
| 137 | +#else |
| 138 | + catch { } |
| 139 | +#endif |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments