Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
Starting with Android 8.0 (Oreo), Google introduced Notification Channels as a new way to provide more fine-grained control over notifications to the users. All notifications must now be categorized into channels based on their content and purpose. Users can then manage preferences for each channel rather than managing all the app's notifications as a single group.
Notification Channel: This represents a unique type of notification you want to display. Each channel is fully customizable and users can change its behavior, deciding how intrusive each type of notification should be.
Importance Levels: These dictate how the notification is presented. For example, high importance notifications are shown everywhere, make noise, and pop up on the screen.
Before sending a notification, you should set up the channel (or channels) your app needs. This is typically done in the startup code (such as Application
or MainActivity
):
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channelId = "YOUR_CHANNEL_ID" val channelName = "Your Channel Name" val importance = NotificationManager.IMPORTANCE_HIGH val channel = NotificationChannel(channelId, channelName, importance).apply { description = "Your Channel Description" } val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.createNotificationChannel(channel) }
After setting up the notification channel, you can send notifications and specify the channel ID:
val notification = NotificationCompat.Builder(this, "YOUR_CHANNEL_ID") .setSmallIcon(R.drawable.your_icon) .setContentTitle("Your Notification Title") .setContentText("Your Notification Text") .setPriority(NotificationCompat.PRIORITY_HIGH) .build() val notificationManager = NotificationManagerCompat.from(this) notificationManager.notify(notificationId, notification)
Here, notificationId
is a unique integer for each notification you send. If you send another notification with the same ID, it'll replace the previous one.
Users have full control over each notification channel. They can:
Once you create a notification channel, you cannot change its importance level nor most of its settings. If you need to make changes, you have to create a new channel with a new ID.
If users change settings for a notification channel, those settings persist across app restarts and even device restarts.
In summary, Notification Channels introduced in Android Oreo provide users with better control over the types of notifications they receive and how they're presented. Developers need to adapt their notification strategies to respect these channels and deliver a more user-centric notification experience.
Notifications in Android Oreo Example Code:
// Create a notification channel (required for Android Oreo and above) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_DEFAULT); NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } // Create and show a simple notification NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id") .setSmallIcon(R.drawable.ic_notification) .setContentTitle("Notification Title") .setContentText("Hello, this is a notification.") .setPriority(NotificationCompat.PRIORITY_DEFAULT); NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this); notificationManagerCompat.notify(1, builder.build());
Android Oreo Notification Group Example:
// Create a summary notification for the group NotificationCompat.Builder summaryBuilder = new NotificationCompat.Builder(this, "channel_id") .setSmallIcon(R.drawable.ic_notification) .setContentTitle("Group Summary") .setGroup("group_key") .setGroupSummary(true); NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this); notificationManagerCompat.notify(0, summaryBuilder.build());
Notification Updates in Android Oreo:
// Update the existing notification NotificationCompat.Builder updatedBuilder = new NotificationCompat.Builder(this, "channel_id") .setSmallIcon(R.drawable.ic_notification_updated) .setContentTitle("Updated Title") .setContentText("This notification has been updated."); notificationManagerCompat.notify(1, updatedBuilder.build());