Browse By

Android: How to create notifications in Android using Android Studio 2.3

Hi,

Today we are going to learn how we can implement notifications in android applications using Android Studio 2.3(you can use any version of android studio or eclipse to implement the same).

What are notifications?

Notifications are the messages related to apps in our device which are shown outside of the activity of the app’s UI. When a notification is triggered it first appears as an icon in the notification area.

We can also reach the desired activity or the activity to which the message belongs, through notifications.

Create a new project using the path File-> New-> New Project and fill in the necessary details with a self-explanatory.

Follow these steps to create notifications using Android Studio.

  1. After creating the project, navigate to your layout file i.e res->activity_main.xml (in this case)
  2. Add the following code to your layout file
    <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="30dp"
            android:text="Click the button below to see a notofication in notication area"
            android:textSize="24sp" />
        <Button
            android:onClick="dothis"
            android:id="@+id/but1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click to see notification"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="80dp"
            android:layout_below="@id/tv1"
            />
  3. Create another activity named Main2Activity.java to land us in this activity when we click on one of our notification.
  4. Add the following code to your java class file (MainActivity.java in this case)
    public  void dothis(View v)
        {
            //Notification.builder is a predefined class used to build the notification objects
            //who's various methods are used to give values to our notification.
            Notification.Builder nm= new Notification.Builder(this);
            nm.setSmallIcon(R.mipmap.ic_launcher);
            nm.setColor(Color.BLUE);
            nm.setContentTitle("This is my first Notification");
            nm.setContentText("Text/Message of this notification");
    
            //setPriority() method is used to set the priority of the notification as we can see
            //in above case that the notification which is created afterward is show above the first
            //notification as we have set it's priority to high.
            nm.setPriority(Notification.PRIORITY_LOW);
    
            Notification.Builder nm1= new Notification.Builder(this);
            nm1.setSmallIcon(R.mipmap.ic_launcher);
            nm1.setColor(Color.GREEN);
            nm1.setContentTitle("This is my second Notification");
            nm1.setContentText("Text/Message of this notification");
            nm1.setPriority(Notification.PRIORITY_HIGH);
            Intent i= new Intent(this,Main2Activity.class);
            PendingIntent pi= PendingIntent.getActivity(this,0,i,0);
            nm.addAction(R.mipmap.ic_launcher,"Way to notification's Activity",pi);
    
            //NotificationManager is the class used to notify the user of an event that happened or triggered.
            NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            Notification n1=nm.build();
            Notification n12= nm1.build();
            notificationManager.notify(0,n1);
            notificationManager.notify(1,n12);
    
        }
  5. PendingIntent is used to take us to the desired activity from the notification until and unless we click on the notification and this action is performed using the addAction() method.
  6. On following the above steps and running the application, below outputs should be observed
    pulse7-notifications-images
    On clicking the “CLICK TO SEE NOTIFICATION” is clicked we’ll see two icons in notification panel and we can swipe them down to see the following output
    pulse7-notifications-image
    On clicking the “WAY TO NOTIFICATION’S ACTIVITY” we’ll be landed to another activity.
    pulse7-notifications-image

I hope you like this article. Share your reviews to improve content. Please stay tune with us.

Don’t forget to subscribe for more articles.

Happy Coding!