How to create Notification in android studio

What is the use of Notifications?

How to create Notification in Android Studio:- Notification (Notification) is a more distinctive feature in the Android system. When the user sends out some prompt information, and the application is not running in the foreground, it can be realized with the help of notifications. Send out one after a notification, a notification icon will be displayed in the status bar at the top of the phone. After you pull down the status bar, you can see the details of the notification.

 


Basic usage of notification in Android:

Now that we understand the basic concepts of notification in android, let’s take a look at how to use notifications in android. Notification usage is still relatively flexible, it can be created either in the activity or in the broadcast receiver.

Compared with broadcast receivers and services, there are still fewer scenarios for creating notifications in events. Because generally we only need to use notifications in android when the program enters the background.

However, no matter where you create a notification, the overall steps are the same. Let’s learn how to create a notification in android studio. Know the detailed steps. First, you need a NotificationManager to manage notifications in android, you can call the Context Obtained by the getSystemService() method. The getSystemService() method receives a string parameter to determine the acquisition system Which service of the system, we can pass in NotificationManager.class here. Therefore, get An instance of NotificationManager can be written as:



 

Next, you need to create a Notification object, which is used to store various information required for notification in android. You can use its parameterized constructor to create it. The parameterized constructor of Notification can be written as:

The NotificationCompat.Builder method of Notification can set a standard layout for the notification in android. This method receives four parameters, the first parameter is ContentTitle, there is nothing to explain about this. The second parameter is used to specify the ContentText of the notification You can see this part of the content by pulling down the system status bar. The third parameter is used to specify the body content of the notification, also under You can see this part of the content by pulling the system status bar. the fourth parameter is AutoCancel.

After the above work is completed, you only need to call the notify() method of NotificationManager to display the notification Out. The notify() method receives two parameters. The first parameter is id. Make sure that the id specified for each notification is different. The second parameter is the Notification object, here we directly set the Notification object we just created Just pass in. Therefore, displaying a notification can be written as:

So far, we have analyzed every step of creating a notification in android. Let us go through a specific example.

Let’s take a look at what the notification looks like.


Creating Notification in Android Studio:

Open Android Studio and click on the new project

notification in android

Then select empty activity and click on the Next button

notification in android

Then select the name for your application, in my case I select “NotificationDemo” and finally press the finish button

notification in android


As you can our notification project is created successfully

notification in android

Now modify the code in activity_main.xml as follows:

The layout file is very simple, there is only a Send notification button, which is used to send out a notice.

Next modify The code in MainActivity is as follows:

As you can see, we have completed the creation of the notification in the click event of the Send notice button, and the creation process As described earlier. Now you can run the program, click the Send notice button, you will see A notification is displayed in the system status bar, as shown in below Figure.

notification in android

Pull down the system status bar to see the detailed information of the notification, as shown in below Figure.

notification in android

If you have used an Android phone, you should subconsciously think that this notification is clickable. But when you click on it, you will find no effect. No, it seems that there should be a response after each notification is clicked Yes? In fact, if we want to achieve the click effect of the notification in android, we also need to make the corresponding settings in the code, which involves A new concept, PendingIntent.


How to use PendingIntent in android:

PendingIntent looks similar to Intent from the name, and they do have a lot in common. For example, they can all specify a certain “intent”, and can be used to start activities, start services, and send broadcasts. The difference is that Intent is more inclined to perform an action immediately, while PendingIntent is more inclined to Time to perform an action. Therefore, PendingIntent can also be simply understood as a delayed execution Intent.

The usage of PendingIntent is also very simple, it mainly provides several static methods for obtaining PendingIntent Instance, you can choose to use getActivity() method, getBroadcast() method, or getService() according to your needs method. The parameters received by these methods are the same, and the first parameter is still Context, so there is no need to explain more.

The second parameter is generally not used, and usually just pass in 0. The third parameter is an Intent object, we can pass Construct the “intent” of the PendingIntent from this object. The fourth parameter is used to determine the behavior of PendingIntent, there are FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT and FLAG_UPDATE_CURRENT These four values ​​are optional. For the meaning of each value, you can check the document, I will not explain them one by one.



After having a certain understanding of PendingIntent, we will look back at the Notification builder method.

So here You can construct a delayed execution “intent” through PendingIntent, which will be executed when the user clicks on the notification The corresponding logic.

Now let’s optimize the NotificationDemo project, add a click function to the notification just now, and let the user click on it When you can start another activity.

First, you need to prepare another activity. For creating new activity simply right click on package folder then click on new, in new click on Activity, in activity select the Empty Activity

notification in android

Then select the name for new activity in my case I select NotificationActivity

notification in android


Now modify the code of activity_notification.xml as follows:

The content of the layout file is very simple, with only one TextView displayed in the center, which is used to display a piece of text information. Then create a new NotificationActivity inherited from Activity, load the layout file just defined here, the code is as follows:


Then modify the code in AndroidManifest.xml and add the registration statement of NotificationActivity in it, As follows:

So that the NotificationActivity activity is ready, let’s modify the code in MainActivity, Add a click function to the notification in android, as shown below:

As you can see, we first use Intent to express our “intent” to start NotificationActivity, but Then pass the constructed Intent object to the getActivity() method of PendingIntent to get the PendingIntent.

Now run the program again and click the Send notice button, a notice will still be sent out. Then pull down the system In the status bar, click on the notification and you will see the interface of NotificationActivity, as shown in the below video.



The final output of Notification in android:

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

1 Comment

Leave a Reply

%d bloggers like this: