One of great functionalities that apps from nowadays used are the notifications, which can be use for a lot of different things, from using it when an update of your app comes, to use it to reply a message and many other things you can do with the notifications, while you are is or is not being used.
In this section you will learn how to implement this functionality and use it in your applications.
How can I create notifications in my app?
Before you start using the notifications, make sure that you have the dependencies to use “NotificationCompat”, that is the object that will grant us the option to add those notifications, you can do that by adding the following to our app/build.gradle file:
After that you will be able to to start creating the notifications in our classes.
As I mentioned, “NotificationCompat” will be the object to create, you can do this in your “onCreate” function (in this case I created the object outside of any function and then initiated inside the function:
For our object we will need to give as an input a context and a “channelID” for this last one we need to create it:
Also, as you can see you can set to the notification, the icon with one of our image from the resources folder, the title of the notification that will appear in big and the add a text that can be used to explain the notification, add style to it, the priority, with “setAutoCancel” you can choose if you want the notification to disappear or not when you click on it, and choose where do you want to send you when the notification is clicked with “setContentIntent” but you need to give as an input, an intent to the function so you can created with another activity of our app:
Now everything is ready and you just need to call the notification to appear whenever you want like this:
NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((id of the notification),(“NotificationCompat” object).build());
At this point if you try to open your app in a device it will pop-up the notification and do all the action you established.
So as you can see implement notifications to our apps is simple and very helpful to notify our user with important information even when they are not using you application and as always there is a lot more you can do with this functionality, for that you can go to Android’s official developers website and learn more!