Step by Step Guide to Implementing Native Android Notifications with Code - devshafique/Native-Android-Development-Blogs GitHub Wiki
How to Build a Custom Notification in Android with Java
Looking for a practical guide to native android notifications? Get hands-on experience with code examples. Read now for a successful implementation.
Creating custom notifications in Android can enhance the user experience and add a personal touch to your app. Notifications can provide important information, updates, and reminders to users, even when the app is not actively in use. In this article, we will guide you through the steps to build a custom notification in Android using Java.
Setting up the Notification Channel
Before we dive into building a custom notification, it is important to set up a notification channel. Notification channels are a way to categorize and manage the different types of notifications that your app can send. In Android 8.0 (Oreo) and higher, each notification must belong to a channel and channels must be registered before they can be used.
To set up a notification channel, follow these steps:
- Create a NotificationChannel object with a unique ID, name, and importance level.
- Register the notification channel using the NotificationManager.
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
Building the Notification
Now that we have set up the notification channel, we can start building the notification. To build a notification, we need to create a NotificationCompat.Builder object and specify the necessary elements, such as the small and large icons, the title and content text, and the priority level.
private void buildNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
Adding Action Buttons
Notifications can also include action buttons that allow users to interact with the notification and perform quick actions, such as snoozing an alarm or marking a task as complete. To add action buttons to a notification, use the addAction method on the NotificationCompat.Builder object.
private void buildNotificationWithAction() {
Intent snoozeIntent = new Intent(this, SnoozeReceiver.class);
PendingIntent snoozePendingIntent = PendingIntent.getBroadcast(this, 0, snoozeIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.addAction(R.drawable.ic_snooze, getString(R.string.snooze), snoozePendingIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
Customizing the Notification Layout
In addition to the basic elements, notifications can also be customized with different layout options, such as expanded and big text styles. To use these styles, create a NotificationCompat.Style object and pass it to the setStyle method on the NotificationCompat.Builder object.
private void buildExpandedNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Much longer text that cannot fit one line..."))
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.large_icon))
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
Conclusion
In this article, we have covered the steps to build a custom notification in Android using Java. We have set up a notification channel, built a basic notification, added action buttons, and customized the notification layout. With these tools, you can create personalized and interactive notifications that enhance the user experience in your app.