Android Notification
Android Notification provides short, timely information about the action happened in the application, even it is not running. The notification displays the icon, title and some amount of the content text.
Set Android Notification Properties
The properties of Android notification are set using NotificationCompat.Builder object. Some of the notification properties are mentioned below:
- setSmallIcon(): it sets the icon of notification.
- setContentTitle(): it is used to set the title of notification.
- setContentText(): it is used to set the text message.
- setAutoCancel(): it sets the cancelable property of notification.
- setPriority(): it sets the priority of notification.
Android Notification Example
In this example, we will create a notification message and clicking on it launches another activity.
activity_main.xml
Add the following code in an activity_main.xml file.
Create an activity named as activity_notification_view.xml and add the following code. This activity will launch on clicking the notification. TextView is used to display the notification message.
activity_notification_view.xml
MainActivity.java
Add the following code in the MainActivity.java class. In this class, clicking the button calls the addNotification() method where we implement the NotificationCompat.Builder object to set the notification properties. The NotificationManager.notify() method is used to display the notification. The Intent class is used to call another activity (NotificationView.java) on taping the notification.
NotificationView.java
The NotificationView.java class receives the notification message and displays in TextView. This class is invoked while taping the notification.
strings.xml
AndroidManifest.xml
Add the following code in AndroidManifest.xml file.
Output: