Tuesday, July 7, 2015

Push Notifications in ASP.NET Web API with PushBots.NET

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

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:
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!

16 comments:

  1. 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

    ReplyDelete
  2. Microsoft 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.
    Thanks,
    DOTNET Training in Chennai | DOTNET course in Chennai | DOTNET Training Institute in Chennai

    ReplyDelete
  3. Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

    iOS Training in Chennai

    ReplyDelete
    Replies
    1. is the getdevicebyalias function working?

      Delete
  4. is the getdevicebyalias function working?

    ReplyDelete
  5. i tried this but later got to know that the getdevicebyalias function is excluded from the core pushbots library, please correct me if im wrong

    ReplyDelete
  6. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android training in chennai
    Ios training in chennai

    ReplyDelete

  7. You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...

    Android App Development Company

    ReplyDelete
  8. great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
    web design Company
    web development Company
    web design Company in chennai
    web development Company in chennai
    web design Company in India
    web development Company in India

    ReplyDelete
  9. Nice it seems to be good post... It will get readers engagement on the article since readers engagement plays an vital role in every blog.i am expecting more updated posts from your hands.
    iOS App Development Company
    iOS App Development Company

    ReplyDelete
  10. This article is very much helpful and i hope this will be an useful information for the needed one. Keep on updating these kinds of informative things...
    Fitness SMS
    Fitness Text
    Salon SMS
    Salon Text
    Investor Relation SMS
    Investor Relation Text

    ReplyDelete
  11. it is really amazing...thanks for sharing....provide more useful information...
    Mobile app development company

    ReplyDelete
  12. great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
    web design Company
    web development Company
    web design Company in chennai
    web development Company in chennai
    web design Company in India
    web development Company in India

    ReplyDelete
  13. Nice it seems to be good post... It will get readers engagement on the article since readers engagement plays an vital role in every blog.i am expecting more updated posts from your hands.
    iOS App Development Company
    Android App Development Company

    ReplyDelete
  14. This article is very much helpful and i hope this will be an useful information for the needed one. Keep on updating these kinds of informative things...
    Texting API
    Text message marketing
    Digital Mobile Marketing
    Sms API
    Sms marketing

    ReplyDelete
  15. Thanks for sharing such informative article. Know about Know about English to Tamil from techfizy.

    ReplyDelete