Monday 29 November 2021

Notifications in Android application

In android, Notification is a message which is used to alert the users about some events that happening in our app.

In android, we can alert the users about our app notifications in different forms like a flash the LED or make sounds or display an icon in the status bar, etc. Generally, the android Notifications will be displayed outside of our app’s normal UI and alert the users without interrupting their current activities.


Here we are going to use NotificationCompat class to implement notification in our android application. The NotificationCompat class supports different types of notification views, such as normal view, big view and it provides the best support for a wide range of platforms. 


To create a notification, we need to specify the UI content and required actions with a
NotificationCompat.Builder object. To display an icon, title and detailed text of notification we need to set the following properties in Builder object.
 
setSmallIcon() : To set the small icon for our notification.
setContentTitle() : To set the title of our notification.
setContentText() : To set the detailed text to display in notification.

The above-mentioned properties are necessary to display a notification and we can set a different type of properties to our notification like
setStyle, setSound, setLights, setLargeIcon, etc. based on our requirements using Builder object.
 

Here is the example of creating a notification using
NotificationCompat.Build object and setting the notification properties.


NotificationCompat.Builder nBuilder =  new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.notification_icon)
                .setContentTitle("Notification Title")
                .setContentText("Hi, This will be the notification content");




If we assign an action to the notification, it will allow users to go directly from the notification to an activity of our app. We can also add buttons to the notification to perform additional actions such as hang up the call or responding immediately to a text message; this feature is available as of Android 4.1.

In android, we can define an action inside of notification by using
PendingIntent object which contains an Intent that starts an Activity of our app.

Here is the example of defining an action inside of notification using the PendingIntent object.

NotificationCompat.Builder nBuilder =  new NotificationCompat.Builder(this)
Intent resultIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);
nBuilder.setContentIntent(pendingIntent);




Once we are done with creation of notification, we need to pass a notification to the system by using NotificationManager.notify() method and we need to specify a ID in the notification to use this ID to update a notification later if required.

Here is the example of sending a notification to the system using the Notificationmanager.notify() method.

NotificationCompat.Builder nBuilder =  new NotificationCompat.Builder(this);
int mNotificationId = 999;
NotificationManager mNotifyMgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, nBuilder.build());

No comments:

Post a Comment