Skip to content

Commit 867f2a1

Browse files
committed
Implement deleting notification from OneSignal
1 parent 4bbc20f commit 867f2a1

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

src/SimpleToDoService/Common/PushNotificationScheduler.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,25 @@ public async System.Threading.Tasks.Task SchedulePushNotification(Task task)
2525
if (currentNotification?.DueDate == task.CheckedTargetDate())
2626
return;
2727

28-
DeletePushNotification(currentNotification);
28+
await DeletePushNotification(currentNotification);
2929
await CreatePushNotification(task);
3030
}
3131

32-
void DeletePushNotification(PushNotification notification)
32+
async System.Threading.Tasks.Task DeletePushNotification(PushNotification notification)
3333
{
3434
if (notification == null)
3535
return;
36+
37+
var url = String.Format("https://onesignal.com/api/v1/notifications/{0}?app_id={1}",
38+
notification.ServiceId.ToString(), Environment.GetEnvironmentVariable("ONE_SIGNAL_KEY"));
39+
var request = WebRequest.Create(url) as HttpWebRequest;
40+
41+
request.Method = "DELETE";
42+
request.Headers["authorization"] = String.Format("Basic {0}", Environment.GetEnvironmentVariable("ONE_SIGNAL_KEY"));
43+
44+
await request.GetResponseAsync();
45+
46+
repository.DeletePushNotification(notification);
3647
}
3748

3849
async System.Threading.Tasks.Task CreatePushNotification(Task task)

src/SimpleToDoService/Database/Repositiory.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public interface IToDoRepository
2121
User UpdateUser(User user);
2222
PushNotification CreatePushNotification(PushNotification notification);
2323
IEnumerable<PushNotification> PushNotifications(Task task);
24+
bool DeletePushNotification(PushNotification notification);
2425
}
2526

2627
public class ToDoRepository : IToDoRepository
@@ -148,5 +149,21 @@ public IEnumerable<PushNotification> PushNotifications(Task task)
148149
return context.PushNotifications.Where(o => o.TaskUuid == task.Uuid);
149150

150151
}
152+
153+
public bool DeletePushNotification(PushNotification notification)
154+
{
155+
context.PushNotifications.Remove(notification);
156+
157+
try
158+
{
159+
context.SaveChanges();
160+
}
161+
catch
162+
{
163+
return false;
164+
}
165+
166+
return true;
167+
}
151168
}
152169
}

0 commit comments

Comments
 (0)