Skip to content

Commit b53d2a0

Browse files
committed
Push API
1 parent a53d10d commit b53d2a0

33 files changed

+897
-22
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ UpgradeLog*.htm
221221
*.ldf
222222
*.ndf
223223

224+
# SQLite files
225+
*.db
226+
224227
# Business Intelligence projects
225228
*.rdl.data
226229
*.bim.layout
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Demo.AspNetCore.PushNotifications.Services.Abstractions
2+
{
3+
public interface IPushNotificationService
4+
{
5+
string PublicKey { get; }
6+
7+
void SendNotification(PushSubscription subscription, string payload);
8+
}
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace Demo.AspNetCore.PushNotifications.Services.Abstractions
5+
{
6+
public interface IPushSubscriptionStore
7+
{
8+
Task StoreSubscriptionAsync(PushSubscription subscription);
9+
10+
Task DiscardSubscriptionAsync(string endpoint);
11+
12+
Task ForEachSubscriptionAsync(Action<PushSubscription> action);
13+
}
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Demo.AspNetCore.PushNotifications.Services.Abstractions
2+
{
3+
public enum PushEncryptionKeyName
4+
{
5+
P256DH,
6+
Auth
7+
}
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Demo.AspNetCore.PushNotifications.Services.Abstractions
2+
{
3+
public class PushNotificationServiceOptions
4+
{
5+
public string Subject { get; set; }
6+
7+
public string PublicKey { get; set; }
8+
9+
public string PrivateKey { get; set; }
10+
}
11+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Collections.Generic;
2+
3+
namespace Demo.AspNetCore.PushNotifications.Services.Abstractions
4+
{
5+
public class PushSubscription
6+
{
7+
public string Endpoint { get; set; }
8+
9+
public IDictionary<string, string> Keys { get; set; }
10+
11+
public string GetKey(PushEncryptionKeyName keyName)
12+
{
13+
string key = null;
14+
15+
if (Keys != null)
16+
{
17+
string keyNameStringified = StringifyKeyName(keyName);
18+
19+
if (Keys.ContainsKey(keyNameStringified))
20+
{
21+
key = Keys[keyNameStringified];
22+
}
23+
}
24+
25+
return key;
26+
}
27+
28+
public void SetKey(PushEncryptionKeyName keyName, string key)
29+
{
30+
if (Keys == null)
31+
{
32+
Keys = new Dictionary<string, string>();
33+
}
34+
35+
Keys[StringifyKeyName(keyName)] = key;
36+
}
37+
38+
private string StringifyKeyName(PushEncryptionKeyName keyName)
39+
{
40+
return keyName.ToString().ToLowerInvariant();
41+
}
42+
}
43+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.0.1" />
9+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.1" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\Demo.AspNetCore.PushNotifications.Services.Abstractions\Demo.AspNetCore.PushNotifications.Services.Abstractions.csproj" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
3+
4+
namespace Demo.AspNetCore.PushNotifications.Services.Sqlite
5+
{
6+
internal class PushSubscriptionContext : DbContext
7+
{
8+
public class PushSubscription : Abstractions.PushSubscription
9+
{
10+
public string P256DH
11+
{
12+
get { return GetKey(Abstractions.PushEncryptionKeyName.P256DH); }
13+
14+
set { SetKey(Abstractions.PushEncryptionKeyName.P256DH, value); }
15+
}
16+
17+
public string Auth
18+
{
19+
get { return GetKey(Abstractions.PushEncryptionKeyName.Auth); }
20+
21+
set { SetKey(Abstractions.PushEncryptionKeyName.Auth, value); }
22+
}
23+
24+
public PushSubscription()
25+
{ }
26+
27+
public PushSubscription(Abstractions.PushSubscription subscription)
28+
{
29+
Endpoint = subscription.Endpoint;
30+
Keys = subscription.Keys;
31+
}
32+
}
33+
34+
public DbSet<PushSubscription> Subscriptions { get; set; }
35+
36+
public PushSubscriptionContext(DbContextOptions<PushSubscriptionContext> options)
37+
: base(options)
38+
{ }
39+
40+
protected override void OnModelCreating(ModelBuilder modelBuilder)
41+
{
42+
EntityTypeBuilder<PushSubscription> pushSubscriptionEntityTypeBuilder = modelBuilder.Entity<PushSubscription>();
43+
pushSubscriptionEntityTypeBuilder.HasKey(e => e.Endpoint);
44+
pushSubscriptionEntityTypeBuilder.Ignore(p => p.Keys);
45+
}
46+
}
47+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
namespace Demo.AspNetCore.PushNotifications.Services.Sqlite
5+
{
6+
public static class SqliteApplicationBuilderExtensions
7+
{
8+
public static IApplicationBuilder UseSqlitePushSubscriptionStore(this IApplicationBuilder app)
9+
{
10+
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
11+
{
12+
PushSubscriptionContext context = serviceScope.ServiceProvider.GetService<PushSubscriptionContext>();
13+
context.Database.EnsureCreated();
14+
}
15+
16+
return app;
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)