Skip to content

Commit 67ce06f

Browse files
committed
VAPID tokens caching
1 parent 19bf894 commit 67ce06f

File tree

8 files changed

+60
-9
lines changed

8 files changed

+60
-9
lines changed

Demo.AspNetCore.PushNotifications.Services.Abstractions/Demo.AspNetCore.PushNotifications.Services.Abstractions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Lib.Net.Http.WebPush" Version="1.0.0" />
8+
<PackageReference Include="Lib.Net.Http.WebPush" Version="1.1.0" />
99
</ItemGroup>
1010

1111
</Project>

Demo.AspNetCore.PushNotifications.Services.PushService/Demo.AspNetCore.PushNotifications.Services.PushService.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
<LangVersion>latest</LangVersion>
55
</PropertyGroup>
66
<ItemGroup>
7-
<PackageReference Include="Lib.Net.Http.WebPush" Version="1.0.0" />
7+
<PackageReference Include="Lib.Net.Http.WebPush" Version="1.1.0" />
8+
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.0.0" />
9+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.0.0" />
810
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" />
911
</ItemGroup>
1012
<ItemGroup>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using Microsoft.Extensions.Caching.Memory;
3+
using Lib.Net.Http.WebPush.Authentication;
4+
5+
namespace Demo.AspNetCore.PushNotifications.Services.PushService
6+
{
7+
internal class MemoryVapidTokenCache : IVapidTokenCache
8+
{
9+
private readonly IMemoryCache _memoryCache;
10+
11+
public MemoryVapidTokenCache(IMemoryCache memoryCache)
12+
{
13+
_memoryCache = memoryCache;
14+
}
15+
16+
public string Get(string audience)
17+
{
18+
if (!_memoryCache.TryGetValue(audience, out string token))
19+
{
20+
token = null;
21+
}
22+
23+
return token;
24+
}
25+
26+
public void Put(string audience, DateTimeOffset expiration, string token)
27+
{
28+
_memoryCache.Set(audience, token, expiration);
29+
}
30+
}
31+
}
Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Microsoft.Extensions.Options;
1+
using System;
2+
using Microsoft.Extensions.Options;
3+
using Microsoft.Extensions.Logging;
24
using Lib.Net.Http.WebPush;
35
using Lib.Net.Http.WebPush.Authentication;
46
using Demo.AspNetCore.PushNotifications.Services.Abstractions;
@@ -10,24 +12,36 @@ internal class PushServicePushNotificationService : IPushNotificationService
1012
private readonly PushNotificationServiceOptions _options;
1113
private readonly PushServiceClient _pushClient;
1214

15+
private readonly ILogger _logger;
16+
1317
public string PublicKey { get { return _options.PublicKey; } }
1418

15-
public PushServicePushNotificationService(IOptions<PushNotificationServiceOptions> optionsAccessor)
19+
public PushServicePushNotificationService(IOptions<PushNotificationServiceOptions> optionsAccessor, IVapidTokenCache vapidTokenCache, ILogger<PushServicePushNotificationService> logger)
1620
{
1721
_options = optionsAccessor.Value;
1822

1923
_pushClient = new PushServiceClient
2024
{
2125
DefaultAuthentication = new VapidAuthentication(_options.PublicKey, _options.PrivateKey)
2226
{
23-
Subject = _options.Subject
27+
Subject = _options.Subject,
28+
TokenCache = vapidTokenCache
2429
}
2530
};
31+
32+
_logger = logger;
2633
}
2734

2835
public void SendNotification(PushSubscription subscription, string payload)
2936
{
30-
_pushClient.RequestPushMessageDeliveryAsync(subscription, new PushMessage(payload)).Wait();
37+
try
38+
{
39+
_pushClient.RequestPushMessageDeliveryAsync(subscription, new PushMessage(payload)).Wait();
40+
}
41+
catch (Exception ex)
42+
{
43+
_logger?.LogError(ex, "Failed requesting push message delivery to {0}.", subscription.Endpoint);
44+
}
3145
}
3246
}
3347
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.Extensions.DependencyInjection;
2+
using Lib.Net.Http.WebPush.Authentication;
23
using Demo.AspNetCore.PushNotifications.Services.Abstractions;
34

45
namespace Demo.AspNetCore.PushNotifications.Services.PushService
@@ -7,6 +8,8 @@ public static class PushServiceServiceCollectionExtensions
78
{
89
public static IServiceCollection AddPushServicePushNotificationService(this IServiceCollection services)
910
{
11+
services.AddMemoryCache();
12+
services.AddSingleton<IVapidTokenCache, MemoryVapidTokenCache>();
1013
services.AddSingleton<IPushNotificationService, PushServicePushNotificationService>();
1114

1215
return services;

Demo.AspNetCore.PushNotifications.Services.Sqlite/Demo.AspNetCore.PushNotifications.Services.Sqlite.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Lib.Net.Http.WebPush" Version="1.0.0" />
8+
<PackageReference Include="Lib.Net.Http.WebPush" Version="1.1.0" />
99
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.0.1" />
1010
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.1" />
1111
</ItemGroup>

Demo.AspNetCore.PushNotifications/Demo.AspNetCore.PushNotifications.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
<TargetFramework>netcoreapp2.0</TargetFramework>
44
</PropertyGroup>
55
<ItemGroup>
6-
<PackageReference Include="Lib.Net.Http.WebPush" Version="1.0.0" />
7-
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
6+
<PackageReference Include="Lib.Net.Http.WebPush" Version="1.1.0" />
7+
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
88
</ItemGroup>
99
<ItemGroup>
1010
<ProjectReference Include="..\Demo.AspNetCore.PushNotifications.Services.Abstractions\Demo.AspNetCore.PushNotifications.Services.Abstractions.csproj" />

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Sample project for demonstrating Push Notifications based on Push API and Web Pu
44

55
- [Push API](https://www.tpeczek.com/2017/12/push-notifications-and-aspnet-core-part.html)
66
- [Requesting Delivery](https://www.tpeczek.com/2018/01/push-notifications-and-aspnet-core-part.html) ([Lib.Net.Http.WebPush](https://github.com/tpeczek/Lib.Net.Http.WebPush))
7+
- VAPID tokens caching
78

89
## Donating
910

0 commit comments

Comments
 (0)