RunCodes
Programming & Tech

How to Create Notification in Android using Android Studio?

2 669

Notification in Android

Before directly implementing the notification code in your android studio, let’s understand little about notification:

A notification is a message you can display to the user outside of your application’s normal UI. When you tell the system to issue a notification, it first appears as an icon in the notification area.
To see the details of the notification, the user opens the notification drawer. Both the notification area and the notification drawer are system-controlled areas that the user can view at any time.

Example of notification:

Notification in Android

 

Overall process

  • We specify the UI information and actions for a notification in a NotificationCompat.Builder object.
  • To create the notification itself, we call NotificationCompat.Builder.build(), which returns a Notification object containing our specifications.
  • To issue the notification, we pass the Notification object to the system by calling NotificationManager.notify().

Now let separate it in four steps:

 

[sociallocker]

Step1: create the notification as:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)

Step2: Set Notification Properties/ Required Notification Contents:

A Notification object must contain the following:

  A small icon, set by setSmallIcon()

  A title, set by setContentTitle()

  Detail text, set by setContentText()

Step 3: Attach Actions

This is an optional part and required if we want to attach an action with the notification. An action allows users to go directly from the notification to an Activity in our application using PendingIntent().

Step 4: Issue the Notification
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManger.notify(notificationID, buider.build());

Now let’s come to the point.

you need to watch the following videos for the designing part of the notification.

After watching the designing part do the following things:

  1. declare the button as: Button _btnnotify
  2. initialize the button as: _btnnotify=(Buttion)findViewByID(R.id.btnnotify).
  3. Create the listening event of button and write the following code inside the listening event of button as:

_btnnotify.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {

NotificationCompat.Builder builer = new NotificationCompat.Builder(MainActivity.this);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://techsupportnep.blogspot.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 01, intent,0);
builer.setContentIntent(pendingIntent);
builer.setDefaults(Notification.DEFAULT_ALL);
builer.setContentTitle("NOTIFICATION TILE HERE");
builer.setSmallIcon(R.mipmap.ic_launcher);
builer.setContentText("this is notification example");
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(001, builer.build());

}
});

[/sociallocker]

Video:

 

 

Show Comments (2)