Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/adessoturkey-dotnet/adspush

AdsPush is a server-side push notification library designed to work seamlessly across the most common .NET platforms. With full support for APNS (Apple Push Notification Service), FCM (Firebase Cloud Messaging), and VAPID WebPush, it offers a comprehensive solution for your push notification needs. Engineered for both ease of use and flexibility,
https://github.com/adessoturkey-dotnet/adspush

apns apple-push-notifications fcm firebase firebase-cloud-messaging notification-api notification-service notifications push-notification push-notification-android push-notification-server push-notifications

Last synced: about 2 months ago
JSON representation

AdsPush is a server-side push notification library designed to work seamlessly across the most common .NET platforms. With full support for APNS (Apple Push Notification Service), FCM (Firebase Cloud Messaging), and VAPID WebPush, it offers a comprehensive solution for your push notification needs. Engineered for both ease of use and flexibility,

Awesome Lists containing this project

README

        

# AdsPush

**AdsPush** is the server-side push notification library that fully supports APNS(Apple Push Notification Service) and FCM (Firebase Cloud Messaging) features and works with the the most common .NET platforms. It puts together good abstraction, easy using and full support for advanced use cases.

- [NuGet](https://www.nuget.org/packages/AdsPush)
- [Report Bug or Request Feature](https://github.com/adessoTurkey-dotNET/AdsPush/issues)
- [Contact Me Via Mail](mailto:[email protected]?subject=AdsPush)
- [Contact Me Via Linkedin](https://www.linkedin.com/in/anıl-dursun-şenel)

# Table Of Content

1. [Features](#features)
2. [Get It Started](#get-it-started)
3. [Configuration](#configuration)
1. [Microsoft Dependency Injection](#microsoft-dependency-injection)
2. [Using Sender Instance](#using-sender-instance)
4. [Sending notifications](#sending-notifications)

# Features
1. Abstraction sender works with APNS & FCM
2. Full support for all functionality platform specific parameters.
3. Support up-to-date recommended APIs.
4. Easy to use.
5. Advanced configuration options

# Get It Started
1. Install AdsPush by using the following command

` dotnet add package AdsPush ` from dotnet cli

or ` Install-Package AdsPush ` from package console

2. Decide using platform (APNS or/and FCM) and get the required configuration files from the portal.
3. Apply the following instructions to configure and send notifications.

# Configuration
You have two easy options to be able configure AdsPush

1. Using Microsoft Dependency Injection (recommended)
- Using default configuration provider (Microsoft Options Pattern)
- Using custom configuration provider.
2. Using direct sender instance.

### Microsoft Dependency Injection

Microsoft Dependency Injection is Microsoft's IOC library coming with .NET Core. AdsPush supports using MDI to be able to manage your push configuration and sending operations.

If you're sing .NET 6 or newer version in `Program.cs`

```csharp

using AdsPush.Extensions;

var builder = WebApplication.CreateBuilder(args);
//Option 1:From configuration
builder.Services.AddAdsPush(this.Configuration);

//Option 2:From Action
builder.Services.AddAdsPush(options =>
{
//Your configurations
});

//Option 3:From custom provider that is implementation of IAdsPushConfigurationProvider interface.
builder.Services.AddAdsPush();
```
If you're sing .NET 5 or any .NET Core version in `Startup.cs`

```csharp
using AdsPush.Extensions;
...

public override void ConfigureServices(IServiceCollection services)
{
//your code...

//Option 1:From configuration
services.AddAdsPush(this.Configuration);

//Option 2:From Action
services.AddAdsPush(options =>
{
//Your configurations
});

//Option 3:From custom provider that is implementation of IAdsPushConfigurationProvider interface.
services.AddAdsPush();

}
```

And put the following section in your in your `appsettings.[ENV].json`

```
{
"Logging": {
...
},
"AdsPush": {
"MyApp": {
"TargetMappings": {
"Ios": "Apns",
"Android": "FirebaseCloudMessaging",
"BrowserAndPwa": "VapidWebPush"
},
"Apns": {
"P8PrivateKey": "",
"P8PrivateKeyId": "<10 digit p8 certificate id. Usually a part of a downloadable certificate filename>",
"TeamId": "",
"AppBundleIdentifier": "",
"EnvironmentType": ""
},
"FirebaseCloudMessaging": {
"Type":"",
"ProjectId":"",
"PrivateKey": "",
"PrivateKeyId": "",
"ClientId": "",
"ClientEmail": "",
"AuthUri": "",
"AuthProviderX509CertUrl": "",
"TokenUri": "",
"ClientX509CertUrl": ""
},
"Vapid": {
"PublicKey": "",
"PrivateKey": "",
"Subject": ""
}
}
}
...
}
```

If you wish to use host/pod environment or any secret provider you can set the following environment variables.

```
AdsPush__MyApp__Apns__AppBundleIdentifier=
AdsPush__MyApp__Apns__EnvironmentType=
AdsPush__MyApp__Apns__P8PrivateKey=
AdsPush__MyApp__Apns__P8PrivateKeyId=<10-digit p8 certificate id; often part of a downloadable certificate filename>
AdsPush__MyApp__Apns__TeamId=<10-digit Apple team id shown on the Apple Developer Membership Page>
AdsPush__MyApp__FirebaseCloudMessaging__AuthProviderX509CertUrl=
AdsPush__MyApp__FirebaseCloudMessaging__AuthUri=
AdsPush__MyApp__FirebaseCloudMessaging__ClientEmail=
AdsPush__MyApp__FirebaseCloudMessaging__ClientId=
AdsPush__MyApp__FirebaseCloudMessaging__ClientX509CertUrl=
AdsPush__MyApp__FirebaseCloudMessaging__PrivateKey=
AdsPush__MyApp__FirebaseCloudMessaging__PrivateKeyId=
AdsPush__MyApp__FirebaseCloudMessaging__ProjectId=
AdsPush__MyApp__FirebaseCloudMessaging__TokenUri=
AdsPush__MyApp__FirebaseCloudMessaging__Type=
AdsPush__MyApp__TargetMappings__Android=FirebaseCloudMessaging
AdsPush__MyApp__TargetMappings__BrowserAndPwa=VapidWebPush
AdsPush__MyApp__TargetMappings__Ios=Apns
AdsPush__MyApp__Vapid__PrivateKey=
AdsPush__MyApp__Vapid__PublicKey=
AdsPush__MyApp__Vapid__Subject=

```

Now, you can easily use wia DI as the. following.

```csharp
private readonly IAdsPushSender _pushSender;
public MyService(
IAdsPushSenderFactory adsPushSenderFactory)
{
this._pushSender = adsPushSenderFactory.GetSender("MyApp");
}

```

### Using Sender Instance

The following lines of codes can be used without any DI configuration.

```csharp

using AdsPush;
using AdsPush.Abstraction;
using AdsPush.Abstraction.Settings;

var builder = new AdsPushSenderBuilder();
var apnsSettings = new AdsPushAPNSSettings()
{
//put your configurations hare.
};

var firebaseSettings = new AdsPushFirebaseSettings()
{
//put your configurations hare.
};

var vapidSettings = new AdsPushVapidSettings()
{
//put your configurations hare.
};

var sender = builder
.ConfigureApns(apnsSettings, null)
.ConfigureFirebase(firebaseSettings, AdsPushTarget.Android)
.ConfigureVapid(vapidSettings, null)
.BuildSender();

```

## Sending notifications

When you obtain `IAdsPushSender` instance by using one the methods shown above, you're ready to send notification. The following sample code can be used trigger a basic notification request.

```csharp

var basicPayload = new AdsPushBasicSendPayload()
{
Title = AdsPushText.CreateUsingString("test"),
Detail = AdsPushText.CreateUsingString("detail"),
Badge = 52,
Sound = "default",
Parameters = new Dictionary()
{
{
"pushParam1", "value1"
},
{
"pushParam2", "value2"
},
}
};

var apnDeviceToken = "15f6fdd0f34a7e0f46301a817536f0fb1b2ab05b09b3fae02beba2854a1a2a16";
//var apnDeviceTokenVapid = "{"endpoint:"...", "keys": {"auth":"...","p256dh":"..."}}";

await sender.BasicSendAsync(
AdsPushTarget.Ios,
apnDeviceToken,
basicPayload);

//For VAPID WebPush with multi parametere
string
endpoint = "https://fcm.googleapis.com/fcm/send/cIo6QJ4MMtQ:APA91bEGHCpZdHaUS7otb5_xU1zNWe6TAqria9phFm7M_9ZIiEyr0vXj3gRHbeIJMYvp2-SAVbgNrVvl7uBvU_VTLpIA0CLBcmqXuuEktGr0U4LVLvwWBibO68spJk7D-lr8R9zPyAXE",
p256dh = "BIjydse4Rij892SJN10xx1qbxDM6GrYXSfg7TGu90CVM1WmlTYzn_79psRqseyWdER969LGLjZmnXIhHPaKTyGE",
auth = "TkLGLzFeUU3C9SJJN6dLAA";

var subscription = VapidSubscription.FromParameters(endpoint, p256dh, auth);
await sender.BasicSendAsync(
AdsPushTarget.BrowserAndPwa,
subscription.ToAdsPushToken(),
basicPayload);

```

If you wish to access whole supported parameters of the related platform, the following methods can be helpful.

```csharp

//sample for Apns
var apnsResult = await sender
.GetApnsSender()
.SendAsync(
new APNSRequest()
{
ApnsPayload = new()
{
Badge = 52,
Sound = "",
MutableContent = true,
FilterCriteria = "",
ThreadId = "",
TargetContentId = "",
Alert = new APNSAlert()
{
Title = "",
Body = "",
Subtitle = ""
}
//more...
},
AdditionalParameters = new Dictionary()
{
{"pushParam1", "value1" },
{"pushParam2", "value2"},
{"pushParam3", 52},
}
},
"79eb1b9e623bbca0d2b218f44a18d7b8ef59dac4da5baa9949c3e99a48eb259a",
Guid.NewGuid());

//sample for FCM
var firebaseResult = await sender
.GetFirebaseSender()
.SendToSingleAsync(new Message()
{
Token = "",
Android = new AndroidConfig()
{
Priority = Priority.High,
},
Notification = new()
{
Title = "",
Body = "",
ImageUrl = ""
}
});

//Sample for VAPID WebPush
var vapidResult = await sender
.GetVapidSender()
.SendAsync(
subscription,
new VapidRequest()
{
Title = "",
Badge = "",
Message = "",
Sound = "",
Icon = "",
Image = "",
Language = "",
Silent = false,
Tag = "",
ClickAction = "",
VibratePattern = "",
Data = new Dictionary()
{
{"param1", "value1"}
}
});

```