Different types of notifications in Android systems
15 minute(s) read | Published on: May 02, 2021 Updated on: Dec 14, 2021 |
They design notifications to remind users of important things to be informed of important things. Notifications include text, titles, buttons, styles, images, or icons. There are different types of notifications, which we will discuss in this section of educational articles.
Notifications are, in fact, the notification windows that inform users of important things. It should be noted that notifications are initially displayed as icons in the Status Bar that if the user pulls the bar down and Notification Drawer opens, the notification will be displayed in full. Notice can be generated and displayed during the day, so there is no need to run the program, and even if the program is not running, the relevant notification will appear. It should be noted that even notifications can be displayed on the lock screen and can be displayed from there.
As mentioned, there are different types of notifications, and they can be created using different methods. Some of them can be simple or complex, with images, icons, text, title, buttons, and so on. For example, we can use PendingIntent to define an operation that the user can perform after touching the notification.
1- We create a new project in Android Studio with the desired name Notification.
2- The activity used in this project is better selected as Empty Activity.
3- Add a Button to the activity_main.xml file. Note: Layers have been changed to LinearLayout for easy management.
By inserting the above code into the MainActivity class, we create a Button called simpleButton and an object from the NotificationManager method called notifManager. Then we define these two in onCreate.
To create the contents of notices, we need to create an object from the NotificationCompat.Builder method, we name this object sNotifBuilder. It should be noted that the input parameter of this method is Context.
setSmallIcon:
Using this method, we can set the notices icon. In other words, the icon of all notices is determined using this method. To select different types of icons and use them for notices, right-click on the drawable folder and then go to New> Vector Asset. On the page that opens after completing the path, you must select an icon and add it to be finally added to the drawable. The name of the icon we have added in this project is notif_icon. Note: It should be noted that the quality of screens on Android systems has increased, so it is better to be careful in choosing icons and do not choose icons that have the extension jpg or png. Instead, use icons that are vector and of high quality.
Of course, if you want to use icons that do not have Android inside, you can copy the desired icon and paste it into this folder.
setContentTitle:
This method is used to specify the message title. If we want to specify a title for the notification message, I use this method.
setContentText:
This method is also used to display the text of the notification message.
We also send two inputs to the notifManager object using the notify method. The first input of this object is an integer that represents the ID of each notification. It should be noted that the amount of this input must be unique.
In the second input, we also build sNotifBuilder.
We run the project.
After execution, we will see that the notification has been applied correctly. First, the notification will appear as an icon in the status bar, and then by dragging it down, the entire content inside the notification will be displayed. To delete this notification from the status bar, simply drag it left or right.
Add Notification Channel to Android Oreo
It should be noted that notices have changed in Android 8 or Android Oreo and must be provided in the form of channels to better manage notices.
For example, consider a store with a variety of notices. Notices such as new products or daily discounts. If the user does not want to receive product notices and can only receive discount notices, if his device is Android Oreo, he can apply this to finally send discount notices.
1- We create a Channel and introduce it to the notification:
It should be noted that each channel must have a unique id and name, reject this part of the tutorial in the code listed above to be able to define the id for the channel, we have defined a String with the desired name offerChannelId, the value of which is also Offers.
To create NotificationChannel we added a function with the desired name createNotifChannel. As mentioned above, notification channels are only required for Oreo and higher versions and do not need to be defined as lower versions. To do this, I enter a code that has a condition, such as the following:
Entering the above code first checks the operating system and the operating system of the user's device, and if the operating system is equal to the O version, if Oreo or greater, the redacted code will execute the trace inside the block. Otherwise, the codes listed above will not be executed.
Therefore, in the code listed above, we set a condition to check if the operating system version is equal to or greater than the O version. If Oreo, create the channel and execute the code listed above.
In the code above, a name for channel B is given as offerChannelName, and a description for them is given as offerChannelDescription.
Finally, we create notifChannel using the createNotificationChannel method and send it to notifManager. In addition, we add createNotifChannel to the onClick button.
Now is the time to run the project again.
After the run, we will see that the notification we wanted was successfully created, and we got the result we wanted.
Then click on Manage notifications:
In the first step, the name of our desired program is displayed in the list, which has two modes of on or off, and we can enable or disable the notices related to it by turning it on or off and saying that the notices related to that display to be given or not. Importance and Priority features In the previous section, we set importance, the same degree of which was defined for the channel with the value IMPORTANCE_DEFAULT. This level of importance can be defined in four levels: - IMPORTANCE_MIN, which is the lowest level of importance.
- IMPORTANCE_LOW, which is the average level of channel importance.
IMPORTANCE_DEFAULT, which is the high or default level of importance.
- IMPORTANCE_HIGH, which is the highest level and level of importance or in other words, it can be said that it shows the emergency level of notices.
Given that each of the notices has a level and degree of importance, by selecting each of them, we inform the users how important the displayed notification is. It should be noted that each of the notices, considering their level and degree of importance, has special features that we can use to inform the user how important and necessary the notification is.
For example, if we set the notification to IMPORTANCE_LOW, the notification will not have an icon. Also, no sound will be played to notify the user. This notification will be displayed in the Notification Drawer with this degree of importance,. The user can only see it in the Notification Drawer and will not have any sound or icons. But the notification with the degree of importance IMPORTANCE_DEFAULT has an icon that will be displayed, in addition to the icon will also have a sound with which to inform the user. This shows that the notification is very important and must be checked.
In other words, notices with the same degree of IMPORTANCE_HIGH will be higher than other notices in the Notification Drawer.
These features are applied to notices using set Priority. Like the following:
Here are some other features that can be used for NotificationCompat.Builder:
setVibrate:
The first thing that applies to notices is setVibrate, which creates a vibration when notifications run to notify the user. How to run and apply vibration when notices are generated is as follows:
Numbers represent the time that we can adjust the duration of the vibration by inserting the desired numbers. It should be noted that the numbers are in milliseconds. The format of the numbers and how to set the duration is as follows:
It should be noted that when we first defined, or in this example, the same 100, is for the delay in creating vibration. The next numbers from left to right show the duration of vibration execution and vibration stop, respectively. In the example and codes above, we specify that the vibration after 100 milliseconds will be delayed for 500 milliseconds and three times after the notification is executed, and a pause of 500 milliseconds will occur after each execution of the notification vibration.
- setLargeIcon:
This is used to place a very large icon and image related to the notification. However, an icon and its associated image will be displayed next to the notification.
- SetSound:
They are used to create and apply sound to any of the notices.
- setVisibility:
This method has three different modes, which are as follows:
1- VISIBILITY_PUBLIC:
In this case of this method, the notification and the icon and sound appear in the Lock screen and can be easily displayed.
2- VISIBILITY_PRIVATE:
In this case, this notification method is displayed in the Lock screen, but its title and icon are hidden, and its icon and title cannot be viewed until the screen is unlocked.
3- VISIBILITY_SECRET:
Defined. Like the following:
- enableVibration:
This is also used to enable vibration for notices. This method is the same as setVibrate, except that ,you cannot set intervals for the vibration delay or the time it takes to do so in this method. However, it should be noted that this method can also be used to apply vibration and vibration.
- Enable Lights:
This method is also used to activate the LED light at the top of the screen by setting it to true. So, we use this method to turn on the screen LED when creating a notification.
- setLightColor:
We also use this method to determine the color of the LED light. Like the following:
Full code MainActivity.java:
It should be noted that in this example, we have considered green for the color of LED lights.
Push notification
In this part of the educational article, we will get acquainted with notification push.
What is a notification push?
Push notices are notices specified by app developers or device builders for applications installed on the device and system. These devices can be mobile phones, tablets, phablets, etc.
Push notices can appear when the device communicates with the server, so the user can receive them when connected to the Internet. Because in this case, the connection between the device and the server is established, and the user can easily receive related notices.
Why use push notices?
It is worth mentioning that finding and attracting users to buy, download and use our applications is part of the job. The biggest part is related to users' use of that application permanently. For example, consider that you have downloaded several applications and installed them on your device, but you may have used them once and forget that you have such an application installed on your device. In this case, you do not use these applications, and the purpose is not just to buy, download and install them on the device but the purpose is to use them.
Therefore, notifications can also be used to remind and advertise those applications. Most push notices appear to introduce and advertise to attract users to use the application. In other words, push notices are one of the marketing methods that can also perform introduction and advertising operations and attract users to use applications installed on the system or download some applications.
Users do not need to open the program to see the push notices. Still, by connecting to the server and the Internet, the connection between the server and the device is easily established, and users can view the push notices.
What are the advantages of push notices over other notifications?
There are many marketing methods today, the most common of which are sending text messages, emails, or push notices that appear to users when they connect to the Internet and the server. Each of these methods has its characteristics, advantages, and disadvantages, but it should be noted that the advantages of these marketing methods outweigh their disadvantages. For example, with the appearance of push notices, you can easily introduce applications to users and encourage them to use them. Or in other cases, they can be encouraged to buy and download different types of applications.
1- Cost:
It should be noted that the cost of sending and launching push notifications is much lower than other methods, and in some cases, it may even have no cost and be free. In this way, push notification services are provided to application managers. In this case, application managers are free until a certain number is sent, and after that, they are paid less until another certain number is sent.
2- Visit rate:
It is worth mentioning that the notification rate of push notifications is higher than other advertising methods, and many users are attracted to push notifications. According to statistics, many users are attracted to push notifications and review them. The rate of viewing and checking emails and the like is very low compared to push notifications. Message messages and emails may be sent when users cannot see them, but push notifications are sent when the device is connected to the Internet. This means that notifications are sent when the device is in front of users, and users can easily see them. But users may not see or check emails and messages.
3- Possibility of personalizing the message:
In this method of marketing, there is a possibility of personalization. In the case that push notifications are in front of users and users can easily see them, the possibility of attracting users by push notifications is more than other methods. It is also possible to apply some features in push notifications. For example, to increase notifications' notification rate and click rate, its settings can be customized to attract more users. For example, it is possible to consider and apply discounts in notifications. For example, you can apply settings and personalization to display push notifications related to user searches.
In this tutorial, you will learn how to make a simple notification. If you follow all the steps in this tutorial carefully, you will create and execute notifications. We also learned how to use NotificationChannel, compatible with Android O and higher versions.
About our Android and iOS app development services