UniversalPushNotification 1.0.5
dotnet add package UniversalPushNotification --version 1.0.5
NuGet\Install-Package UniversalPushNotification -Version 1.0.5
<PackageReference Include="UniversalPushNotification" Version="1.0.5" />
<PackageVersion Include="UniversalPushNotification" Version="1.0.5" />
<PackageReference Include="UniversalPushNotification" />
paket add UniversalPushNotification --version 1.0.5
#r "nuget: UniversalPushNotification, 1.0.5"
#:package UniversalPushNotification@1.0.5
#addin nuget:?package=UniversalPushNotification&version=1.0.5
#tool nuget:?package=UniversalPushNotification&version=1.0.5
UniversalPushNotification
UniversalPushNotification is a .NET 8-based library that provides a simple and flexible solution for managing web push notifications. The library allows you to send notifications to specific users or user groups, making it ideal for targeted communication in web applications.
Features
- Full .NET 8 support
- User and group management
- VAPID authorization
- Asynchronous operations
- Comprehensive logging support
- Seamless integration with existing projects
Installation
Using NuGet Package Manager Console:
Install-Package UniversalPushNotification
Using .NET CLI:
dotnet add package UniversalPushNotification
Configuration
1. VAPID Configuration
Add VAPID configuration to your appsettings.json:
{
"VapidKeys": {
"PublicKey": "your_public_key",
"PrivateKey": "your_private_key",
"Subject": "mailto:your@email.com"
}
}
2. Service Registration
In your Program.cs file, add:
builder.Services.AddUniversalPushNotifications();
Usage
Injecting the Notification Service
public class NotificationController : ControllerBase
{
private readonly IPushNotificationService _pushService;
public NotificationController(IPushNotificationService pushService)
{
_pushService = pushService;
}
}
User Subscription
[HttpPost("subscribe")]
public async Task<IActionResult> Subscribe([FromBody] PushSubscription subscription)
{
subscription.UserId = User.Identity?.Name;
await _pushService.SaveSubscriptionAsync(subscription);
return Ok();
}
Sending Notification to a Specific User
var payload = new NotificationPayload
{
Title = "New Notification",
Message = "Your order is ready",
Data = new Dictionary<string, string>
{
{ "orderId", "123" }
}
};
await _pushService.SendNotificationAsync(payload, "userId");
Sending Notification to a Group
await _pushService.SendNotificationToGroupAsync(payload, "admins");
Client-Side Implementation
Service Worker Registration
if ('serviceWorker' in navigator && 'PushManager' in window) {
const registration = await navigator.serviceWorker.register('/sw.js');
const subscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: 'your_public_key'
});
await fetch('/api/notifications/subscribe', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(subscription)
});
}
Service Worker Configuration (sw.js)
self.addEventListener('push', function(event) {
const data = event.data.json();
const options = {
body: data.message,
icon: data.icon,
data: data.data,
actions: data.actions
};
event.waitUntil(
self.registration.showNotification(data.title, options)
);
});
Advanced Examples
Group Management
// Adding a user to a group
await _pushService.AddUserToGroupAsync("userId", "admins");
// Sending notification to a group
var payload = new NotificationPayload
{
Title = "Admin Notification",
Message = "New request requires approval"
};
await _pushService.SendNotificationToGroupAsync(payload, "admins");
Debugging
The library utilizes the standard .NET logging system. Configure ILogger in your application to view logs. Example configuration:
services.AddLogging(builder =>
{
builder.AddConsole();
builder.AddDebug();
});
Security Considerations
VAPID Key Security
- Store VAPID keys securely
- Never expose private keys in client-side code
- Rotate keys periodically
Protocol Security
- Always use HTTPS in production
- Implement proper user authorization
- Validate all incoming subscription data
Best Practices
- Implement rate limiting for notifications
- Handle subscription expiration
- Monitor failed delivery attempts
Contributing
To contribute to the project:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Building from Source
git clone https://github.com/yourusername/UniversalPushNotification.git
cd UniversalPushNotification
dotnet build
dotnet test
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
For support and questions:
- Create an issue in the GitHub repository
- Email: support@example.com
- Documentation: [link to documentation]
Contact
Nikoloz Kuridze - niko.quridze@gmail.com
Project Link: https://github.com/nikolozkuridze/UniversalPushNotification
Acknowledgments
- Web Push Protocol
- .NET Community
- Contributors and testers
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- System.Security.Cryptography.Algorithms (>= 4.3.1)
- System.Text.Json (>= 8.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.