Notifications
Notifications
▪ Android Notification provides short, timely information about the
action happened in the application, even it is not running
▪ This application could be running in the background but not in use
by the user.
▪ The purpose of a notification is to notify the user about a process
that was initiated in the application either by the user or the system.
▪ The notification displays the icon, title and some amount of the
content text.
Steps for notifications
Step 1 - Create Notification Builder
▪ As a first step is to create a notification builder using
[Link]().
▪ You will use Notification Builder to set various Notification properties like its
small and large icons, title, priority etc.
[Link] mBuilder = new [Link](this)
Steps for notifications
Step 2 - Setting Notification Properties
▪ Once you have Builder object, you can set its Notification properties using
Builder object as per your requirement.
▪ But this is mandatory to set at least following −
▪ A small icon, set by setSmallIcon()
▪ A title, set by setContentTitle()
▪ Detail text, set by setContentText()
[Link]([Link].notification_icon);
[Link]("Notification Alert, Click Me!");
[Link]("Hi, This is Android Notification Detail!");
Steps for notifications
Step 3 - Issue the notification
▪ you pass the Notification object to the system by calling
[Link]() to send your notification.
▪ Make sure you call [Link]() method on builder object
before notifying it.
▪ This method combines all of the options that have been set and return a new
Notification object.
NotificatioNotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// notificationID allows you to update the notification later on.
[Link](notificationID, [Link]());
Steps for notifications
Step 4 – Create Notification Channel
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Notification"
tools:layout_editor_absoluteX="180dp"
tools:layout_editor_absoluteY="228dp" />
</RelativeLayout>
package [Link]; [Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
String channel_id = "01" ;
Button b = findViewById([Link]) ;
[Link]( new [Link]() {
@Override
public void onClick (View v) {
[Link] mBuilder = new
[Link](MainActivity. this, "default" )
.setSmallIcon([Link])
.setContentTitle( "Notification of Application" )
.setContentText( "This is my first push notification" );
NotificationManager nm = (NotificationManager) getSystemService(Context.
NOTIFICATION_SERVICE ) ;
// checking if android version is greater than oreo(API 26) or not
if ([Link]. SDK_INT >=
[Link].VERSION_CODES. O )
{
NotificationChannel nc = new
NotificationChannel( channel_id , "CHANNEL_01" ,
NotificationManager.IMPORTANCE_HIGH) ;
[Link]( channel_id) ;
[Link](nc) ;
}
[Link](( int ) System. currentTimeMillis (), [Link]()) ;
}
}) ; } }