A client recently asked me to integrate Push Notifications into their Ionic/Cordova App that I’m currently developing for them, so after a bit of searching I found and decided to go with PushBots as my Push Server rather than handling all the APN / GCM registrations and storing of tokens myself.

Their free plan is quite generous and gives you full access to their API, the only downside is they don’t have a .NET Client Library, so I decided to create PushBots.NET which simply wraps their API into a nice little NuGet Package.

In this post I will take you through setting up PushBots.NET and scaffolding your very own API to interact with the PushBots API so that you can integrate it into any existing backend systems you may already have (in my case, my client has an existing system which they use to send their clients SMS’ which they want to now modify to include Push notifications to those who have installed the app)

This post assumes that:

You have an existing app of some kind already (Ionic / Android / iOS etc)

You have registered with PushBots and correctly configured your application according to their docs

Alrighty! Let’s do this

Create an empty Web API Project

So first things first, scaffold your empty Web API project, or jump straight to the next step if you already have a Web API project.

Install PushBots.NET from NuGet

Install PushBots.NET from NuGet by using either the GUI or from the Package Manager Console by typing Install-Package PushBots.NET

a) Using the GUI

b) Using the Package Manager Console

Create a Push Controller

Now let’s create an empty API controller called PushController

The PushBotsClient class takes 2 parameters, AppId and Secret. You can find these in your PushBots Dashboard under your Application Settings -> Keys.

Next we’re going to define two constants where we will set the AppId and Secret, as well as create a readonly field for the PushBotsClient and then instantiate it in our constructor.

Add the following code to your empty controller:

private readonly PushBotsClient _pushBotsClient; private const string AppId = "xxx"; // Use your Application ID found in the PushBots Dashboard private const string Secret = "xxx"; // Use your Secret Key found in the PushBots Dashboard public PushController() { _pushBotsClient = new PushBotsClient(AppId, Secret); }

Create a Post Method

Now we will add a simple Post method which will take an alias and a message. This is a basic example just to send a Single Push Notification (as per the PushBots API docs)

Add the following code to your controller:

public async Task<HttpResponseMessage> Post(string alias, string message) { try { var device = await _pushBotsClient.GetDeviceByAlias(alias); if (device != null) { var pushMessage = new SinglePush { Platform = device.Platform, Token = device.Token, Message = message, Sound = "", Badge = "+1", Payload = new JObject() }; var result = await _pushBotsClient.Push(pushMessage); if (result.IsSuccessStatusCode) { return Request.CreateResponse(HttpStatusCode.OK, result.ReasonPhrase); } return Request.CreateErrorResponse(result.StatusCode, result.Content.ReadAsAsync<JObject>().Result.ToString()); } return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Couldn't find device with alias: " + alias); } catch (Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.GetBaseException().Message); } }

That’s it! You should now be able to hit this endpoint and, assuming you have PushBots configured correctly in your app, start sending notifications to your device(s) from your API!

What’s next?

I have just given a simple example to send a Push Notification to a Single Device, head over to the GitHub repo for a full list of supported methods and usage examples.

Have fun!