Tuesday, November 19, 2013

Push Notification for Android

Hello,

Now a Days in era of smart phones and mobile app developments, a very important feature is "Push Notification".

For any Mobile app (Android, iPhone, Blackberry, etc.) Push Notification is part of requirement and complete project.
To know more about Push notifications please Google !! here is a quick Wikipedia link.




In this post I am going to demonstrate Push notification for Android phones. As of now I don't have any Android device connected with my machine so can't get snapshots of result but one can trust me that whatever I have implemented is fully working.

OK.. So Let's Start ..........

  1.  Create an Asp.net application .(whatever you like..MVC, or anything) 

                       I hope you are aware about NuGet Package Manager. if not refer official website of NuGet. visual Studio 2010 + comes up with inbuilt support of NuGet Packages. still can't find NuGet Packages into your Visual Studio Install from Website.
  2. Once You are done with Above step, Right click on Solution of project. and Select "Manage NuGet Packages for Solution.."

  3. "Manage NuGet Package" window will appear. 
  4.  Thanks to "Redth", who has provided us a very useful NuGet Package Named "PushSharp"search from Right top search window for "PushSharp"  and Select the First Package.


  5. Click on "Install" Button. If Prompts to "Select Project", Select the Projects in which you would like to add reference of PushSharp library.Click OK.
  6. Here we go.. Nuget Package Installation will start and wait until is completes.


  7. Close all other open window. you can track output windows whether installation completed successfully. You can check in Project Reference, whether new references of PushSharp are assigned or not. If everything is good then let's move ahead Or feel free to post your problem. I will try my best to support you.

Now It's Time to create Project for Android Push Notification.

Please Follow the article to Create and Configure http://developer.android.com/google/gcm/gs.html (you might find some changes as recently Google has changed API Console UI. So try to push more efforts to understand)
Alright, I hope You have successfully Created Android API Project. It's very important part for Android push notification. 
You might have come across two important Key works while creating Project.
  1. Project Number : is used to generate Device Token from Android Device. The Project number keeps track of the notification raised for particular device consumes for which Project ID. You have to provide the Project number to Android phone developer. They will use the project number and Provide Device Token.


  2. API key : API key secured key mandatory to pass while processing notifications.



Keep this information somewhere stored.

Now There are three important credentials  required to accomplish Push notification for Android.
  1. Device Token : using Project Number, Android Phone developer can provide the Device Token. I have no Idea how they does.. 
  2. API Key : The API key provided by Project.
  3. Message: The message to be passed on as Push Notification.
Basically, Some implicit or explicit action required to raise the Notification. In My Case I used to created a WCF Restful service (Sample Application). We had a message with minor database parameters to be passed. So Whenever the notification was supposed to raise, phone developers were making a request on my WCF service with Device Token as Parameter.

Here is code block to raise notification. Very short and simple !! :)

var push = new PushBroker();            
push.RegisterGcmService(new GcmPushChannelSettings(APIKey));            
push.QueueNotification(new GcmNotification().ForDeviceRegistrationId(DeviceTokenID)
                .WithJson("{\"message\":\"" + Message + "\"}")); 


That's all. Done..

I have done few more push notification also using PushSharp.
Please feel free to contact me for any kind of assistance.

Post a Comment