AdsPush: Sending Server-Side Push Notifications Using .NET

Anıl Dursun ŞENEL
adessoTurkey
Published in
4 min readAug 11, 2023

In the realm of modern app development, where engagement is the heartbeat of success, the art of seamless communication has taken center stage. Picture the impact of reaching out to your users with timely updates, personalized content, or important announcements, as they go about their digital journey. This is where the power of AdsPush comes into play.

What is AdsPush?

AdsPush is an open-source NuGet package developed with .NET. This package is designed to manage and send push notifications on the server side. It particularly stands out by offering support for both APNS (Apple Push Notification Service) and FCM (Firebase Cloud Messaging), ensuring seamless integration with various platforms. It distinguishes itself with high-level abstraction, ease of use, and full support for advanced usage scenarios.

How to Use It?

Using AdsPush is straightforward and allows for rapid integration. Here are the basic steps:

Step 1: Add the NuGet Package to Your Project

Add the AdsPush package to your project via NuGet Package Manager or .NET CLI:

dotnet add package AdsPush

or

Install-Package AdsPush

Step 2: Configure The Services

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.

Microsoft Dependency Injection is Microsoft’s IoC library that comes with .NET Core. AdsPush supports using MDI to manage your push configuration and sending operations.

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


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<MyProvider>();

If you’re sing .NET 5 or any .NET Core version in Startup.cs

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<MyProvider>();

}

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

{
"Logging": {
...
},
"AdsPush": {
"MyApp": {
"TargetMappings": {
"Ios": "Apns",
"Android": "FirebaseCloudMessaging"
},
"Apns": {
"P8PrivateKey": "<p8 certificate string without any space and start and end tags>",
"P8PrivateKeyId": "<10 digit p8 certificate id. Usually a part of a downloadable certificate filename>",
"TeamId": "<Apple 10 digit team id shown in Apple Developer Membership Page>",
"AppBundleIdentifier": "<App slug / bundle name>",
"EnvironmentType": "<Apns Env one of Development or Production>"
},
"FirebaseCloudMessaging": {
"Type":"<type filed in service_account.json>",
"ProjectId":"<project_id filed in service_account.json>",
"PrivateKey": "<private_key filed in service_account.json>",
"PrivateKeyId": "<private_key_id filed in service_account.json>",
"ClientId": "<client_id filed in service_account.json>",
"ClientEmail": "<client_email filed in service_account.json>",
"AuthUri": "<auth_uri filed in service_account.json>",
"AuthProviderX509CertUrl": "<auth_provider_x509_cert_url filed in service_account.json>",
"TokenUri": "<client_x509_cert_url filed in service_account.json>",
"ClientX509CertUrl": "<token_uri filed in service_account.json>"
}
}
}
...
}

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

AdsPush__MyApp__Apns__AppBundleIdentifier=<App slug / bundle name>
AdsPush__MyApp__Apns__EnvironmentType=<Apns Env one of Development or Production>
AdsPush__MyApp__Apns__P8PrivateKey=<p8 certificate string without any space and start and end tags>
AdsPush__MyApp__Apns__P8PrivateKeyId=<10 digit p8 certificate id. Usually a part of a downloadable certificate filename>
AdsPush__MyApp__Apns__TeamId=<Apple 10 digit team id shown in Apple Developer Membership Page>
AdsPush__MyApp__FirebaseCloudMessaging__AuthProviderX509CertUrl=<auth_provider_x509_cert_url filed in service_account.json>
AdsPush__MyApp__FirebaseCloudMessaging__AuthUri=<auth_uri filed in service_account.json>
AdsPush__MyApp__FirebaseCloudMessaging__ClientEmail=<client_email filed in service_account.json>
AdsPush__MyApp__FirebaseCloudMessaging__ClientId=<client_id filed in service_account.json>
AdsPush__MyApp__FirebaseCloudMessaging__ClientX509CertUrl=<token_uri filed in service_account.json>
AdsPush__MyApp__FirebaseCloudMessaging__PrivateKey=<private_key filed in service_account.json>
AdsPush__MyApp__FirebaseCloudMessaging__PrivateKeyId=<private_key_id filed in service_account.json>
AdsPush__MyApp__FirebaseCloudMessaging__ProjectId=<project_id filed in service_account.json>
AdsPush__MyApp__FirebaseCloudMessaging__TokenUri=<client_x509_cert_url filed in service_account.json>
AdsPush__MyApp__FirebaseCloudMessaging__Type=<type filed in service_account.json>
AdsPush__MyApp__TargetMappings__Android=FirebaseCloudMessaging
AdsPush__MyApp__TargetMappings__Ios=Apns

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

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

2. Using direct sender instance

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


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 sender = builder
.ConfigureApns(apnsSettings, null)
.ConfigureFirebase(firebaseSettings, AdsPushTarget.Android)
.BuildSender();

Sending notifications

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


await sender.BasicSendAsync(
AdsPushTarget.Ios,
"79eb1b9e623bbca0d2b218f44a18d7b8ef59dac4da5baa9949c3e99a48eb259a",
new ()
{
Title = AdsPushText.CreateUsingString("test"),
Detail = AdsPushText.CreateUsingString("detail"),
Badge = 52,
Sound = "default",
Parameters = new Dictionary<string, object>()
{
{"pushParam1","value1"},
{"pushParam2","value2"},
}
});

If you wish to access all the parameters supported by the related platform, the following methods can be helpful:



//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<string, object>()
{
{"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 = ""
}
});

Conclusion: Elevating Engagement with AdsPush

In conclusion, AdsPush offers more than just push notifications; it offers a path to meaningful connections with your app’s users. Seamlessly integrating with APNS and FCM, AdsPush lets you communicate effortlessly across iOS and Android platforms. This open-source gem invites you to contribute on GitHub, collectively shaping the future of engagement.

As developers, we’re not just building apps; we’re crafting experiences. By embracing AdsPush’s power, we amplify communication, create resonance, and strengthen bonds. Let’s embark on this journey together, reshaping how our apps connect and inspire in the digital landscape. Cheers to building apps that resonate and connect in the most elegant way possible.

--

--