Skip to content

Getting Started

tuwrraphael edited this page Sep 21, 2018 · 4 revisions

This example describes how PushServer is configured in an ASP.NET Web Application using EntityFrameworkCore to persist push subscriptions.

Installation

The following packages need to be added to the project:

Install-Package PushServer
Install-Package PushServer.PushConfiguration.EntityFramework
Install-Package PushServer.WebPush

Configuration

ConfigurationStore

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)))

Push provider(s)

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

Storing a subscription

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" } };

Finding subscriptions

All stored subscriptions of a user can be accessed using IPushConfigurationManager.GetAllAsync

await pushConfigurationManager.GetAllAsync(userId)

Requesting notification delivery

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:

  1. subscription id, which is generated by the pushConfigurationManager on RegisterAsync
  2. 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);