UniversalPushNotification 1.0.5

dotnet add package UniversalPushNotification --version 1.0.5
                    
NuGet\Install-Package UniversalPushNotification -Version 1.0.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="UniversalPushNotification" Version="1.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="UniversalPushNotification" Version="1.0.5" />
                    
Directory.Packages.props
<PackageReference Include="UniversalPushNotification" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add UniversalPushNotification --version 1.0.5
                    
#r "nuget: UniversalPushNotification, 1.0.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package UniversalPushNotification@1.0.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=UniversalPushNotification&version=1.0.5
                    
Install as a Cake Addin
#tool nuget:?package=UniversalPushNotification&version=1.0.5
                    
Install as a Cake Tool

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

  1. VAPID Key Security

    • Store VAPID keys securely
    • Never expose private keys in client-side code
    • Rotate keys periodically
  2. Protocol Security

    • Always use HTTPS in production
    • Implement proper user authorization
    • Validate all incoming subscription data
  3. Best Practices

    • Implement rate limiting for notifications
    • Handle subscription expiration
    • Monitor failed delivery attempts

Contributing

To contribute to the project:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.