In this blog, I am going to explain how we can integrate a push notification service for Android using C#. We all know that mobile applications are booming in the market. Basically, push notification is used to give updates to application users.
Jump to Section
Some More About Push Notifications…
A push notification is a message that pops up on a mobile device. App publishers can send them at any point of time. Push notifications look like SMS text messages and mobile alerts, but they only reach users who have installed the app.
Each mobile platform has support for push notifications — iOS, Android, Fire OS, Windows and BlackBerry all have their own services. Push notifications are widely used on all mobile devices to share updated information or events while the user is not actively using the application. On Android gadgets, when a gadget gets a push warning, the sender application’s symbol and a message show up in the status bar. At the point when the client taps the notice, he arrives on the application.
To send push notifications on Android we have to use google GCM. GCM stands for Google Cloud Messaging service. GCM is a free service it can be used to send push notifications to end users. The Google servers involved in taking messages from the third-party application server and sending them to the device.
Google Cloud Messaging is a service that acts as an intermediary between your server and end user’s device. With GCM, Google’s Cloud Connection Server, often referred to as CCS, manages the persistent connections for you. It also makes sure that you push notifications are delivered securely and reliably.
See below picture it describes a pictorial representation of push notification.
To send push notifications, we must have access to below required information:
A.) DeviceId:-This is also known as RegistrationId. It is needed to identify which device the notification has to be sent.
B.) Message: – It contains the notification text.
C.) GoogleAppID: – It is the App Id for the application to be picked for Google project. We must have to register our app on google to get this id. The application that is registering to receive messages. This ensures that the messages are targeted at the correct application.
D.) SenderId: – It is the same as GoogleAppID. The sender ID is used in the registration process to identify an Android application that is permitted to send messages to the device. This ID is typically role-based rather than being a personal account. This ID is generated by the Android developer at user registration time. Save this ID in the database for future use.
Generate GoogleAppID
For creating “GoogleAppID” on GCM server end user must have to follow step below.
1.) Log into https://cloud.google.com/console with your Google account.
2.) In the navigation on the left, go into “APIs & Auth > APIs”.
3.) Find “Google Cloud Messaging for Android” and click the “OFF” button next to it.
4.) In the navigation on the left, go into “APIs & Auth > Registered apps”.
5.) Waits a really long time, apparently.
6.) Click the red REGISTER APP button.
7.) Give the app a name and choose Web Application, then click Register.
8.) Click the “Server Key” section for your server key.
Benefits of Push Notification
Push notifications provide convenience and value to app users. The end user has no need to check their application again and again for a new notification. There are various reasons to use push notifications in your site or app and they have various advantages as far as drawing in with clients and driving activity, notwithstanding, it is basic to utilize them in a way that keeps clients associated, as opposed to irritated.
Push notifications inform users of new information related to your site or an app, providing valuable and relevant updates to customers, even when the site or app is closed. Examples include a message or post on Facebook, a news article update, or a new feature in a game. These are some of the advantages:
1) Fast Delivery
2) Fast Response
3) Location-based Targeting
4) Ease of Use
5) Engaging Users
6) Real Time
For example, users can receive:
A.) Social media notification like what’s app, Facebook and LinkedIn.
B.) Sports scores and news right on their lock screen.
C.) Utility messages like traffic, weather and ski snow reports.
D.) Flight check-in, change, and connection information.
Use source code below:-
Write this code in a separate class file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Collections.Specialized;
public class PushNotification
{
public PushNotification()
{
//
// TODO: Add constructor logic here
//
}
public string SendPushNotification(string deviceId, string message)
{
string GoogleAppID = “**********************”; //Enter google application id.
var SENDER_ID = “**********************”; //Enter Sender id.
var value = message;
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}”, GoogleAppID));
tRequest.Headers.Add(string.Format(“Sender: id={0}”, SENDER_ID));
string postData = “collapse_key=score_update&time_to_live=108 &delay_while_idle=1&data.message=” + value + “&data.time=” + System.DateTime.Now.ToString() + “®istration_id=” + deviceId + “”;
Console.WriteLine(postData);
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();
return sResponseFromServer;
}
}
Note:-Push notifications are sent through “https://android.googleapis.com/gcm/send” web request.
Call above “SendPushNotification” method from your business logic where the user needs a notification. Use below source code to call above method.
using System;
public class TestPushNotification
{
public TestPushNotification()
{
}
public string SentMessage()
{
PushNotification Obj = new PushNotification();
string result = Obj.SendPushNotification(“17BA0791499DB908433B80F37C5FBC89B870084B”, “Hello Manoranjan”);
return result;
}
}
In above code user must have to pass two parameter device id and messages. See below image. It is demo output of push notification.
Concluding Words…
So in this way, we can easily add the push notification features to our apps and get the advantage of this simple but useful feature. I hope this article will help you understand how to implement the Push Notification functionality to your mobile device. Please feel free to use the source code which will help you to integrate.
- LinkedIn Scraper | LinkedIn Data Extractor Software Tool - February 22, 2021
- Is Web Scraping Legal? - February 15, 2021
- What Is Data Scraping? - February 10, 2021
I Am Getting This ”’ The remote server returned an error: (401) Unauthorized.