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
a) Using the GUI
b) Using the Package Manager Console
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:
Add the following code to your controller:
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!
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
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.NETa) 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:
1 2 3 4 5 6 7 8 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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!
Excellent blog and it’s totally loaded with valid posts on Java and .Net technology. Consider including RSS feed in your blog, so aspirants like me can follow your blog easily. .Net Training in Chennai|.asp.net training in chennai
ReplyDeleteMicrosoft develops software called DOTNET. It is a software framework. DOTNET mainly runs on Microsoft windows. It is similar to Java. .Net is most preferred programming language among software developers all over the world. It is also considered as most trusted and effective platform to build high performing desktop or enterprise application.
ReplyDeleteThanks,
DOTNET Training in Chennai | DOTNET course in Chennai | DOTNET Training Institute in Chennai
is the getdevicebyalias function working?
ReplyDeletei tried this but later got to know that the getdevicebyalias function is excluded from the core pushbots library, please correct me if im wrong
ReplyDeleteis the getdevicebyalias function working?
ReplyDeleteThis is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
ReplyDeleteAndroid training in chennai
Ios training in chennai
Thanks for sharing such informative article. Know about Know about English to Tamil from techfizy.
ReplyDelete