How to send Notification from an app in Android Studio.

Hari Lee
3 min readMay 10, 2020

--

A step by step guide in creating a notification channel and pushing notification via BroadcastReceiver.

Create a new project

Select an empty activity for now.

Select the necessary API level on the next page and click finish.

After the build is finished you will see this screen in android studio, which means you successfully created a new project.

Let’s create a broadcast channel.

  1. create a new class and name it App and this class should extend the Activity class. (it should like the next image)
This is how it should look.

2. Copy and paste this code in the App class

Now this will create a notification channel for you to send the notification.

Now we will create the BroadcastRecevier class in which we will write the code for showing the actual notification.

What is BroadcastReceiver?

Android apps can send or receive broadcast messages from the Android system and other Android apps, similar to the publish-subscribe design pattern. These broadcasts are sent when an event of interest occurs. For example, the Android system sends broadcasts when various system events occur, such as when the system boots up or the device starts charging. Apps can also send custom broadcasts, for example, to notify other apps of something that they might be interested in (for example, some new data has been downloaded).

NotifcatinBroadcaster class

When we create this class it will contain only the class definition, but we need to override the onReceive() in it.

this is the method that we need to override. Now we will write the code that will be used to fire a notification.

Here we created a notification builder object I will walk you through the whole process.

a. We created a method in onReceive() called it sendNotification() and passed the context of the activity from which it is called.

b. We create an object for NotificationCompat.Builder class and pass the necessary fields in it, they are

to set the icon : .setSmallIcon(R.drawable.ic_launcher_background)

to set the title : .setContentTitle(“This is title”)

to set the body : .setContentText(“This is the content of the notification”)

to set the priority : .setPriority(NotificationCompat.PRIORITY_DEFAULT)

these are the contents of the notification we are planning to make. Believe it or not, the hard part is over now all we have to do is create a scenario in that will fire the notification.

The use case of this is to show the completion of a certain task or to remind the user of something. The use case here is highly flexible and can be molded to fit any and all scenarios.

This is the MainActivity

here on click of this button, a broadcast intent is passed which will invoke the onReceive() and will cause the notification to be fired.

This is activity_main.xml

This right here is the button that will be clicked.

Before wrapping up, copy and paste this code in the android manifest file.

Now let’s see how well it works, shall we?

Thank you for following along, hope you learned something new today.

--

--

No responses yet