Skip to content

Commit 3a1ecab

Browse files
committed
Add push notification updates when task date changed
1 parent 867f2a1 commit 3a1ecab

3 files changed

Lines changed: 40 additions & 5 deletions

File tree

RequestTests.paw

70 Bytes
Binary file not shown.

src/SimpleToDoService/Common/PushNotificationScheduler.cs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,25 @@ public PushNotificationScheduler(IToDoRepository repository)
1818
this.repository = repository;
1919
}
2020

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+
2135
public async System.Threading.Tasks.Task SchedulePushNotification(Task task)
2236
{
2337
var currentNotification = task.PushNotifications.FirstOrDefault();
2438

25-
if (currentNotification?.DueDate == task.CheckedTargetDate())
39+
if (HasEqualDueDatest(task, currentNotification))
2640
return;
2741

2842
await DeletePushNotification(currentNotification);
@@ -35,13 +49,30 @@ async System.Threading.Tasks.Task DeletePushNotification(PushNotification notifi
3549
return;
3650

3751
var url = String.Format("https://onesignal.com/api/v1/notifications/{0}?app_id={1}",
38-
notification.ServiceId.ToString(), Environment.GetEnvironmentVariable("ONE_SIGNAL_KEY"));
52+
notification.ServiceId.ToString(), Environment.GetEnvironmentVariable("ONE_SIGNAL_APP_ID"));
3953
var request = WebRequest.Create(url) as HttpWebRequest;
4054

55+
request.ContentType = "application/json; charset=utf-8";
4156
request.Method = "DELETE";
4257
request.Headers["authorization"] = String.Format("Basic {0}", Environment.GetEnvironmentVariable("ONE_SIGNAL_KEY"));
4358

44-
await request.GetResponseAsync();
59+
try
60+
{
61+
using (var writer = await request.GetRequestStreamAsync())
62+
{
63+
writer.Write(new Byte[0], 0, 0);
64+
}
65+
await request.GetResponseAsync();
66+
}
67+
#if DEBUG
68+
catch (WebException ex)
69+
{
70+
System.Diagnostics.Debug.WriteLine(ex.Message);
71+
System.Diagnostics.Debug.WriteLine(new StreamReader(ex.Response.GetResponseStream()).ReadToEnd());
72+
}
73+
#else
74+
catch { }
75+
#endif
4576

4677
repository.DeletePushNotification(notification);
4778
}

src/SimpleToDoService/Controllers/TasksController.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ public async System.Threading.Tasks.Task<IActionResult> Post([FromBody] Task ent
7171

7272
var created = repository.CreateTask(entry);
7373

74-
await new PushNotificationScheduler(repository).SchedulePushNotification(created); ;
74+
await new PushNotificationScheduler(repository).SchedulePushNotification(created);
7575

7676
return CreatedAtRoute("GetTask", new { Uuid = created.Uuid }, created);
7777
}
7878

7979
[HttpPut("{uuid:Guid?}")]
80-
public IActionResult Put(Guid? uuid, [FromBody] Task entry)
80+
public async System.Threading.Tasks.Task<IActionResult> Put(Guid? uuid, [FromBody] Task entry)
8181
{
8282
if (!uuid.HasValue)
8383
return BadRequest(new ServiceError() { Message = "Object Uuid not specified" });
@@ -93,6 +93,10 @@ public IActionResult Put(Guid? uuid, [FromBody] Task entry)
9393
if (updated == null)
9494
return NotFound(entry);
9595

96+
var reloaded = repository.Task(CurrentUserUuid, entry.Uuid);
97+
98+
await new PushNotificationScheduler(repository).SchedulePushNotification(reloaded);
99+
96100
return Ok(updated);
97101
}
98102

0 commit comments

Comments
 (0)