Wednesday, 20 July 2016

Android Push Notifications using Google Cloud Messaging (GCM), .Net, SQL Server


Google Cloud Messaging(GCM)


As per Google's Documentation "Google Cloud Messaging for Android is a service that helps developers to send data from server to user's Android Application on Android devices". Using this service you can send your data in a timely manner to update user about new information, services, updates etc.
Integrating Google Cloud Messaging in your Android device provides the more good way to pass information to the user and it saves lots of battery.



Some Basic Concepts of Google Cloud Messaging through SQL Server with C#


In this tutorial, I used C# as server side programming language and SQL Server as database and to code, in a C# I used Visual Studio 2013 as an Integrated Development Environment(IDE).

If you want to know more about GCM so please visit the official document of Google here is a link

There is a simple process to learn how GCM work. Below are the steps :

Step 1: When we install any Any Android application so it will go to GCM for registration and provide Application id to GCM.

Step 2: On successful registration, GCM provides registration id to user Android device.

Step 3: After that our Android device sends this registration id to our server and we are stored that registration id in our database i.e. SQL Server to send notification purpose.

  • When we want to send the notification to a user so we can take user's registration id along with our message and send to GCM Server.
  •  After that rest of the work is done by GCM to send the particular message to a particular user with the help of registration id

Registering with Google Cloud Messaging


Below are the steps to registering google for GCM


Step 1: Go to Google API's console page and login with your google account and create new project (if you already created so it will take you to dashboard)



Step 2: After creating a project you can see the project id in URL. 

Step 3: Go to Mobile API's section and select Google Cloud Messaging from the list.

Step 4: Click on it and enable this API.

Step 5: After this click on Credentials which is coming below.


Step 6: Provide credentials to your project by adding API name in first dropdown i.e. Google Cloud Messaging and Android in your second dropdown and click what credentials do I need


Step 7: After this create an API key by proving name and package name i.e. (your android package name) and SHA-1 certificate fingerprint.




To get a SHA-1 certificate fingerprint so please run this command from cmd and you will get this key.

$ key tool -list -v -keystore mystore.keystore

After putting all this press create API key and you will get your API key please copy this API key.


Next set is to create database and table where you can store Application id of the user to send push notification from the server.

And finally, you can use visual studio to send the request to a GCM for push notification.

Create new project in visual studio


Step 1: Open visual studio (i am using vs 2013 steps are same) to learn how to create new project in visual studio so please visit my last blog "How to create .net project in visual studio"

Step 2: Add new web form in current project and design front end as per your requirement by using this code you can set title, description, an image of a notification.

Step 3: Copy and paste below code and call that method from the button click and fill rest of the fields which is useful.


public void Pushnotification(string registrationid, string titletext, string description, string ImagePath, int ID)
{
       string regid = registrationid;
       var applicationid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
       var senderid = "xxxxxxxxxxxxx";
       var value = titletext;
       var _description = description;
       string _imgpath = imgpath;


       WebRequest tRequest;

       tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");

       tRequest.Method = "post";

       tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";

       tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));


       tRequest.Headers.Add(string.Format("Sender: id={0}", senderid));

       string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.title="
                + value + "&data.img=" + _imgpath + "&data.desc=" + _description + "&data.ID=" + ID + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + regid + "";

            Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            tRequest.ContentLength = byteArray.Length;

            Stream dataStream = tRequest.GetRequestStream();

            dataStream.Write(byteArray, 0, byteArray.Length);

            dataStream.Close();

            WebResponse tResponse = tRequest.GetResponse();

            dataStream = tResponse.GetResponseStream();

            StreamReader tReader = new StreamReader(dataStream);

            String sResponseFromServer = tReader.ReadToEnd();

            tReader.Close();

            dataStream.Close();

            tResponse.Close();
}


Registrationid is a user device id which we can store in the database.
titletext is a title of your notification.
description of your notification.
imagepath is a path of your image which you want to send through push notification.
id is a unique id of your client.

Applicationid is your API key which you are generated in google API's.
SenderID is your project id which you are generated in google API's.




If you have followed above procedure correctly so now you are able to send Android Push Notification through Google Cloud Messaging using .Net.


Please run this project and I'll be happy to solve the errors if you got any.

KEEP CALM and READ ON!!!

Ankur Omar

Saturday, 16 July 2016

Umbarco the complete CMS


Umbraco CMS 


Introduction:

Umbraco is a fully-featured open source content management system with the flexibility to run anything from small campaign or brochure sites right through to complex applications for Fortune 500's and some of the largest media sites in the world.


Umbraco is easy to learn and use, making it perfect for web designers, developers and content creators alike.


How to install or download Umbraco cms 


There are multiple ways to download Umbraco-cms :

1. Install through a NuGet package. Open visual studio goes to tools and open NuGet package and type Umbraco and install it will automatically install all dependencies and make sure to update your web.config file if you install Umbraco in the existing project. 

2. Download Umbraco project through https://our.umbraco.org/  here is a link where you can download the latest version of Umbraco  download link

3. If you want a complete control over Umbraco-cms, So you can download a complete source code of Umbraco from here download link

but it is the little bit tricky to install complete solution of Umbraco so here are the steps to install it completely and working Umbraco solution

Step 1: Download complete solution of Umbraco and unzip it.

Step 2: Install Git into your system if it is not present here is a link. Git is available for different types of an operating system so choose as per your requirement.

Step 3: Open the command prompt in administrator mode and go to your project solution directory and run build.bat file which is present in the build folder.

when you run this from the command prompt so you will see so many packages will install in your project and rest of the missing file will come from server below are the screenshot for your reference



 After completing this your project is ready to work

So open your solution in visual studio and simply press F5 to start when you run this so Umbraco will run in  IIS express and it will open your website in the default browser.

When your website is started so you can get installation page on a screen it will take Name, Email Address, and Password.


The default database of Umbraco-cms is SQL Server Compact, If you want to change database so after filling all details click in customize so new pop up will open just like below image.




In this pop up there are so many options of database are present like Microsoft SQL Server Compact, Microsoft SQL Server, Microsoft SQL Azure, MySQL and Custom connection string choose as per your requirement and click on install

It will open another pop up where you can choose your theme after that your website is ready to work.

If you want to open an admin panel of Umbraco website so simply go to http://localhost/umbraco it will redirect to admin panel

Admin panel contains lots of thing like you can install the theme as per your requirement and install packages or upgrade packages here is a link where you can start learning Umbraco-cms.

Learn more about Umbraco and their packages so please visit https://our.umbraco.org/documentation/


KEEP CALM and READ ON!!!

Ankur Omar

Sunday, 10 July 2016

How to create a Dotnet project


To create a .Net project you need some basic tool to start.


First of all you need any version of Visual Studio IDE (Integrated Development environment) after installing Visual Studio to our local system automatically install .Net framework, Below is an Image of visual studio 2013 when you open visual studio 2013.




After installing when you open a visual studio so it look like above image this is an image of visual studio 2013 that's it installation part is complete.

Some thing about Visual Studio :

"Visual Studio is a complete set of development tools for building ASP.NET Web applications, XML Web Services, desktop applications, and mobile applications. Visual Basic, Visual C#, and Visual C++ all use the same integrated development environment (IDE), which enables tool sharing and eases the creation of mixed-language solutions. In addition, these languages use the functionality of the .NET Framework, which provides access to key technologies that simplify the development of ASP Web applications and XML Web Services."


Visual Studio is a IDE to develop new websites, windows project, web services, windows services etc. This IDE is very simple to understand and develop lots of thing. In market their are so many versions of visual studio is present like visual studio 2013, visual studio 2012, visual studio 2010, visual studio 2008, visual studio 2005 etc  but the latest version of visual studio is visual studio 2015 , So don't worry if you any of this version so you can start.

Here we ready to develop any project in visual studio. 


Step 1 : Open Visual Studio (I am using visual studio 2013 so don't get worry steps are same in any version of visual studio)

Step 2 : Go to file which is present in top left corner - New - New Project. When you click on project new wizard will open just like below image.




As you can see in left side their is so many templates is present like visual basic, visual C#, visual C++ etc, So click on Visual C# and it will open all templates of C# in right side, So select Asp.net Web Application template and provide Project Name, Location and Solution name and click OK.



When you press OK so another wizard will open, This wizard will come in visual studio 2012 and upgrade version of visual studio, If you are not getting this wizard so don't get panic your solution are ready in that case.

Below are the image of wizard



If you get that wizard so go tho empty template and select web form and press OK.


That's it your project is ready to work.

Open Solution Explorer (if you are unable to find solution explorer so go to view and click solution explorer)


In Solution Explorer right click and new pages and start working on it.

Conclusion : In above article i have explained how to start with visual studio. This part contains only basic level of visual studio and in next article i will explain how to create a basic application using asp.net with c# and what type of tool is present in visual studio which have make visual studio a best IDE to development.


KEEP CALM and READ ON!!!

Ankur Omar