PushServiceClient extensions for ASP.NET Core
The PushServiceClient
extensions for ASP.NET Core provide an easy way to configure and create PushServiceClient
instances in an app.
Basic usage
The PushServiceClient
factory (under the hood it uses IHttpClientFactory
) can be registered by calling the AddPushServiceClient
extension method on the IServiceCollection
, inside the Startup.ConfigureServices
method. During registration there is an option to provide default authentication and configuration for a PushServiceClient
.
services.AddPushServiceClient(options =>
{
...
options.PublicKey = "<Application Server Public Key>";
options.PrivateKey = "<Application Server Private Key>";
});
Once registered, code can accept a PushServiceClient
anywhere services can be injected with dependency injection (DI).
internal class PushNotificationsDequeuer : IHostedService
{
private readonly PushServiceClient _pushClient;
public PushNotificationsDequeuer(PushServiceClient pushClient)
{
_pushClient = pushClient;
}
}
VAPID Tokens Caching
There is also an option to enable VAPID tokens caching by calling the AddMemoryVapidTokenCache
or AddDistributedVapidTokenCache
mehod.
services.AddMemoryCache();
services.AddMemoryVapidTokenCache();
services.AddPushServiceClient(options =>
{
...
});
Once a VAPID tokens cache is registered the factory will start using it automatically.