How to create custom Notifications in android studio?
How to create custom Notifications in android studio?
Notifications are an essential part of modern mobile applications. They provide a way to keep users informed about updates, events, and other important information without the need for them to be actively using the app. In Android, notifications are displayed in the notification drawer and can be expanded to show more information.
In this tutorial, we will cover how to create custom notifications in Android Studio using the NotificationCompat library. We will cover the following topics:
- Creating a notification channel
- Building a basic notification
- Adding an action to a notification
- Using styles and custom layouts for notifications
- Displaying a progress bar in a notification
- Creating a notification group
- Handling notification clicks
- Using notification channels for Android 8.0 and above
Before we begin, make sure you have the following software installed:
- Android Studio (latest version)
- Android SDK (minimum SDK version: 16)
Let’s get started!
Creating a notification channel
Notification channels were introduced in Android 8.0 (API level 26) to give users more control over how they receive notifications. A notification channel represents a type of notification and provides a way to set the importance level, sound, vibration, and other properties for that type of notification.
To create a notification channel, we need to define a unique channel ID, a user-visible name for the channel, and a description of what types of notifications will be sent through the channel.
Here’s how to create a notification channel in your MainActivity class:
private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String channelId = "my_channel_id"; CharSequence channelName = "My Channel"; String description = "My Channel Description"; int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel(channelId, channelName, importance); channel.setDescription(description); NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } }
This method checks if the device is running Android 8.0 or higher and then creates a notification channel with the specified channel ID, name, description, and importance level. Note that the importance level determines how the notification will be displayed to the user (e.g., as a heads-up notification, in the notification drawer, etc.).
You can call this method in your onCreate() method to create the notification channel when your app is launched:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); createNotificationChannel(); }
Building a basic notification
Now that we have created a notification channel, let’s build a basic notification. A basic notification consists of a title, a message, and an icon. We will use the NotificationCompat.Builder class to create our notification.
Here’s how to create a basic notification:
private void sendBasicNotification() { String channelId = "my_channel_id"; NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId) .setSmallIcon(R.drawable.ic_notification) .setContentTitle("My Notification") .setContentText("This is a basic notification"); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(1, builder.build()); }
This method creates a new NotificationCompat.Builder object with the specified channel ID and sets the small icon, title, and text for the notification. We then use the NotificationManagerCompat class to display the notification by calling the notify() method with a unique notification ID (in this case, we use 1).
You can call this method from a button click or any other event handler in your app.
Adding an action to a notification
Notifications can also include actions that allow the user to perform a specific task or navigate to a specific screen in your app. To add an action to a notification, we can use the addAction() method of the NotificationCompat.Builder class.
Here’s how to add an action to a notification:
private void sendNotificationWithAction() { String channelId = "my_channel_id"; Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId) .setSmallIcon(R.drawable.ic_notification) .setContentTitle("My Notification") .setContentText("This notification has an action") .addAction(R.drawable.ic_action, "Open", pendingIntent); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(2, builder.build()); }
In this example, we create an Intent that opens the MainActivity when the action is clicked. We then create a PendingIntent using the getActivity() method and pass it to the addAction() method along with the action icon and label.
Using styles and custom layouts for notifications
You can also customize the appearance of your notifications by using styles and custom layouts. Notification styles allow you to display large text, images, and buttons in your notifications. Custom layouts allow you to create a completely custom notification layout.
Here’s an example of using the BigTextStyle to create a notification with a large text:
private void sendBigTextStyleNotification() { String channelId = "my_channel_id"; NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId) .setSmallIcon(R.drawable.ic_notification) .setContentTitle("My Notification") .setContentText("This notification uses BigTextStyle") .setStyle(new NotificationCompat.BigTextStyle() .bigText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed quis felis ultricies, consectetur felis eu, fringilla leo.")) .setPriority(NotificationCompat.PRIORITY_HIGH); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(3, builder.build()); }
In this example, we use the setStyle() method with a new instance of BigTextStyle to set the large text for the notification.
Displaying a progress bar in a notification
You can also display a progress bar in your notifications to indicate the progress of a task. To display a progress bar, we can use the setProgress() method of the NotificationCompat.Builder class.
Here’s an example of using the setProgress() method to display a progress bar in a notification:
private void sendProgressNotification() { String channelId = "my_channel_id"; NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId) .setSmallIcon(R.drawable.ic_notification) .setContentTitle("My Notification") .setContentText("Download in progress") .setProgress(100, 0, false); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(4, builder.build()); }
In this example, we use the setProgress() method to set the maximum progress value (100), the current progress value (0), and whether the progress is indeterminate (false).
Creating a notification group
If your app sends multiple notifications, you can group them together to make it easier for the user to manage and dismiss them. To create a notification group, we can use the setGroup() method of the NotificationCompat.Builder class.
Here’s an example of creating a notification group:
private void sendGroupedNotifications() { String channelId = "my_channel_id"; String groupId = "my_group_id"; NotificationCompat.Builder notification1 = new NotificationCompat.Builder(this, channelId) .setSmallIcon(R.drawable.ic_notification) .setContentTitle("Notification 1") .setContentText("This is notification 1") .setGroup(groupId); NotificationCompat.Builder notification2 = new NotificationCompat.Builder(this, channelId) .setSmallIcon(R.drawable.ic_notification) .setContentTitle("Notification 2") .setContentText("This is notification 2") .setGroup(groupId); NotificationCompat.Builder summaryNotification = new NotificationCompat.Builder(this, channelId) .setContentTitle("2 new notifications") .setContentText("Notification 1 and Notification 2") .setSmallIcon(R.drawable.ic_notification) .setGroup(groupId) .setGroupSummary(true); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(5, notification1.build()); notificationManager.notify(6, notification2.build()); notificationManager.notify(7, summaryNotification.build()); }
In this example, we create two notifications and group them together using the same group ID. We then create a summary notification with a group summary indicator and set the group ID to the same value.
When the notifications are sent, they will appear as a single group with the summary notification at the top.
Conclusion
Notifications are a great way to keep your users informed and engaged with your app. By following the steps above, you can create custom notifications that provide useful information and allow the user to perform specific actions within your app.
Remember to follow best practices for notifications, such as providing clear and concise text, using appropriate icons and colors, and respecting the user’s preferences for notification settings.
I hope this guide has been helpful in learning how to create custom notifications in Android Studio. If you have any questions or comments, feel free to leave them below. Happy coding!
FAQs
1. What is a notification channel in Android?
A notification channel is a way to group notifications by their importance level.
2. What is a notification builder in Android?
A notification builder is a class that is used to build the notification. It contains all the information that needs to be displayed in the notification, including the title, text, and icon.
3. How do I trigger a notification in Android?
You can trigger a notification by calling the createNotification() method from the MainActivity.java file.
4. Can I customize the appearance of a notification in Android?
Yes, you can customize the appearance of a notification by creating a custom layout.
5. Can I schedule notifications in Android?
Yes, you can schedule notifications using the AlarmManager class in Android.