Below I am going to talk about how to create and send a basic notification from your app.
What are Android Notifications?
Notifications are a way to notify user of something significant in a timely manner. These notifications can be generated by the system or by any app. The notifications are important because they enable user to be notified of any information at any time (outside of the regular flow of the app), hence the user doesn’t need to open the app to check for any updates. For e.g. if a user receives a new message or the phone battery is low, the user doesn’t need to explicitly open these apps to realize this new update, but can be notified through a notification.
- Notification Icon appears in the status bar
- When a user pulls down the notification drawer, then the notification is shown with the following properties:
- Notification title
- Time of the notification (inserted by the Android system)
- The actual content of the notification
Create a Basic Notification
Below example generates a simple notification in the method sendNotification, which is called when a button is clicked in the Main Activity.
Important classes :
NotificationCompat, NotificationManagerCompat
public void sendNotification(View v) { NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId); builder.setSmallIcon(R.drawable.notificicon); builder.setContentTitle("You have a notification"); builder.setContentText("This is just for testing a basic notification"); builder.setStyle(new NotificationCompat.BigTextStyle().bigText("This is just for testing a basic notification , also testing if it can be expanded to a big text area." + "Some of my achievements are:" + "* Google certified Associate Android Developer\n" + "* Ring-Message : Android App on Google play store, solved problem of remembering stuff when someone calls/you call someone.\n" + "* Let's Read Them All : Android app on Google play store, solved problem of creating a book playlist and keep a track of unread books, encourages to read more.\n" + "* Humane Eating Project : Android App to encourage and increase awareness about humane eating\n" + "* Roomathon : Android app , won second prize in DamondHacks women hackathon, solved problem of finding roommates when moving to a new city\n" + "* Written an android blog https://androidwithdivya.wordpress.com/ to help others with small android problems which I encountered and learnt while learning everyday.\n" + "* Mozilla Open Source project : Refactored GLES2 implementation for Linux platforms, used by Servo web browser to render graphics, solved Servo's web browser implementation to render graphics on Linux systems\n" + "* ATC Certified Android Application Developer")); }
Note : Generally, notifications are one liner, but if you want the notification to be able to expand to reveal more content, you can use BigTextStyle as shown above to achieve that.
Show the notification
To show the notification to the user, use the NotificationManagerCompat class and its notify method in the following manner:
NotificationManagerCompat manager = NotificationManagerCompat.from(this); manager.notify(unique_id, builder.build());