-
Notifications
You must be signed in to change notification settings - Fork 2
Getting Started
This example describes how PushServer is configured in an ASP.NET Web Application using EntityFrameworkCore to persist push subscriptions.
The following packages need to be added to the project:
Install-Package PushServer
Install-Package PushServer.PushConfiguration.EntityFramework
Install-Package PushServer.WebPush
In Startup.cs configure the PushServer framework.
services.AddPushServer();
In this example, EntityFrameworkCore is used to persist subscriptions. The store is configured using the DbContextOptionsBuilder, e.g.
services.AddPushServer()
.AddConfigurationStore(builder =>
builder.UseSqlServer(connectionString)))
I tend to use SqlLite
var connectionString = $"Data Source={HostingEnvironment.WebRootPath}\\App_Data\\pushservice.db";
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
services.AddPushServer()
.AddConfigurationStore(builder =>
builder.UseSqlite(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly)))
Now the push providers required for this project are configured. This example describes the configuration of a web push (Web Push Protocol) provider.
services.AddPushServer()
.AddConfigurationStore(...)
.AddWebPush(v =>
{
v.PrivateKey = "VapidPrivateKey";
v.PublicKey = "VapidPublicKey";
v.Subject = "mailto:yoursubject.com";
});
For details on VAPID and the Web Push Protocol I recommend this introduction. The keys can be generated using the nodejs webpush cli
npm install web-push -g
web-push generate-vapid-keys --json
When you send the push subscription details from your client application to your web app, they can be stored by using IPushConfigurationManager
. To identify the subscriptions owner, a string userId
must be provided.
[Route("api/[controller]")]
public class PushSubscriptionController : ControllerBase
{
private readonly IPushConfigurationManager pushConfigurationManager;
public PushSubscriptionController (IPushConfigurationManager pushConfigurationManager)
{
this.pushConfigurationManager = pushConfigurationManager;
}
[HttpPost()]
[Authorize]
public async Task<IActionResult> Register([FromBody]WebPushChannelRegistration registration)
{
...
await pushConfigurationManager.RegisterAsync(userId, registration);
...
}
To be able to identify a subscription later on, the pushChannelOptions, key value pairs can be supplied, e.g.
registration.Options = new Dictionary<string,string>() { { "device", "usersmartphone" } };
All stored subscriptions of a user can be accessed using IPushConfigurationManager.GetAllAsync
await pushConfigurationManager.GetAllAsync(userId)
Now that a subscription is stored, you can request notification delivery using IPushService.Push
. There are two possibilites to identify push subscriptions a notification should be sent to:
- subscription id, which is generated by the pushConfigurationManager on
RegisterAsync
- userId and key value pairs of attributes accessed with subscriptions.
await pushService.Push("configurationId", "push notification payload", null);
await pushService.Push("userId",
new Dictionary<string,string>() { { "device", "usersmartphone" } },
"push notification payload", null);