Android Notifications

Android Notifications (BigTextStyle, BigPictureStyle, InboxStyle)

(BigTextStyle, BigPictureStyle, InboxStyle)

In android, Notification is a message which is used to alert the customers approximately a few events that going on in our app.

Generally, the Notifications may be displayed outside of our app’s normal UI and alert the users with out interrupting their present day activities.

In android, we will alert the users about our app notifications in exclusive forms like flash the LED or make a sounds or show an icon in status bar, and so on.

When we tell the gadget to issue a notification, first it’s going to show an icon in notification bar like as shown beneath.

Android

To see the details of our android app notification, we want to open the notification drawer like as proven below.

Android Notifications

Now we are able to see a way to create and difficulty a notifications in android programs with examples.

Here we are going to use NotificationCompat elegance to put in force a notification in our utility. The NotificationCompat elegance supports a extraordinary form of notification views, which includes regular view, big view and it gives a quality support for a extensive variety of structures.

Create a Notification

To create a notification, we want to specify the UI content and required moves with a NotificationCompat.Builder item. To display an icon, title and unique textual content of notification we want to set following homes in Builder object.

  • SetSmallIcon() – It is used to set the small icon for our notification.
  • SetContentTitle() – It is used to set the identify of our notification.
  • SetContentText() – It is used to set the particular text to show in notification.

The above mentioned properties are essential to display a notification and we are able to set a exceptional sort of houses to our notification like setStyle, setSound, setLights, setLargeIcon, and so forth. Primarily based on our requirements the usage of Builder object.

Following is the instance of making a notification using NotificationCompat.Build object and putting the notification houses.

NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle(“Sample notification”) .setContentText(“Hi, Welcome to Tutlane.com”);

Define the Android Notification Actions

If we assign an action to the notification, it’s going to permit customers to head immediately from the notification to an pastime of our app. We can also add buttons to the notification to perform an additional moves consisting of hold up the decision or responding without delay to a text message; this option is available as of four.1.

In android, we will define an movement internal of notification through the usage of PendingIntent item which contains an Intent that begins an Activity of our app.

Following is the instance of defining an motion inner of notification the use of 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);

Issue the Notification

Once we’re carried out with introduction of notification, we need to pass a notification to the gadget by using NotificationManager.Notify() approach and we want to specify a ID in the notification to use this ID to update a notification later if required.

Following is the instance of sending a notification to the device using Notificationmanager.Notify method.

NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this); …. int mNotificationId = 999; NotificationManager mNotifyMgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); // Builds the notification and issues it. mNotifyMgr.notify(mNotificationId, nBuilder.build());

Now we are able to see a way to create and display the notification in android software with instance.

Notification Example

Following is the example of implementing a Notifications in android software.

Create a new android application the use of studio and supply names as NotificationExample. In case in case you aren’t privy to creating an app in android studio check this newsletter Hello World App.

Now open an activity_main.Xml file from reslayout route and write the code like as proven under

activity_main.xml