Skip to content

Commit d1c5444

Browse files
committed
Change sending notification to fire-and-forget
1 parent 01b3ba7 commit d1c5444

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using Lib.Net.Http.WebPush;
1+
using System.Threading.Tasks;
2+
using Lib.Net.Http.WebPush;
23

34
namespace Demo.AspNetCore.PushNotifications.Services.Abstractions
45
{
56
public interface IPushNotificationService
67
{
78
string PublicKey { get; }
89

9-
void SendNotification(PushSubscription subscription, PushMessage message);
10+
Task SendNotificationAsync(PushSubscription subscription, PushMessage message);
1011
}
1112
}

Demo.AspNetCore.PushNotifications.Services.PushService/PushServicePushNotificationService.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading.Tasks;
23
using Microsoft.Extensions.Options;
34
using Microsoft.Extensions.Logging;
45
using Lib.Net.Http.WebPush;
@@ -32,11 +33,11 @@ public PushServicePushNotificationService(IOptions<PushNotificationServiceOption
3233
_logger = logger;
3334
}
3435

35-
public void SendNotification(PushSubscription subscription, PushMessage message)
36+
public async Task SendNotificationAsync(PushSubscription subscription, PushMessage message)
3637
{
3738
try
3839
{
39-
_pushClient.RequestPushMessageDeliveryAsync(subscription, message).Wait();
40+
await _pushClient.RequestPushMessageDeliveryAsync(subscription, message);
4041
}
4142
catch (Exception ex)
4243
{

Demo.AspNetCore.PushNotifications/Controllers/PushNotificationsApiController.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ public async Task<IActionResult> SendNotification([FromBody]PushMessageViewModel
5454
};
5555

5656
// TODO: This should be scheduled in background
57-
await _subscriptionStore.ForEachSubscriptionAsync((PushSubscription subscription) => _notificationService.SendNotification(subscription, pushMessage));
57+
await _subscriptionStore.ForEachSubscriptionAsync((PushSubscription subscription) =>
58+
{
59+
// Fire-and-forget
60+
_notificationService.SendNotificationAsync(subscription, pushMessage);
61+
});
5862

5963
return NoContent();
6064
}

0 commit comments

Comments
 (0)