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
Notifications are an essential feature in most Android applications. They provide a way to inform users about important events, even when they are not actively using the app. Here's a step-by-step guide on how to create a basic notification in Android:
If you are using AndroidX, make sure you have the following dependency in your build.gradle
file:
implementation 'androidx.core:core-ktx:1.X.X'
In your AndroidManifest.xml
, make sure to include the permission for notifications:
<uses-permission android:name="android.permission.INTERNET" />
Starting from Android 8.0 (Oreo), you must create a notification channel before posting any notification. You typically set this up in your application's startup code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channelId = "example_channel_id" val channelName = "Example Channel" val importance = NotificationManager.IMPORTANCE_DEFAULT val channel = NotificationChannel(channelId, channelName, importance).apply { description = "This is an example notification channel." } val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.createNotificationChannel(channel) }
Here's how to create and show a notification:
val notificationId = 101 // This should be a unique ID for each notification val intent = Intent(this, TargetActivity::class.java).apply { flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK } val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, 0) val builder = NotificationCompat.Builder(this, "example_channel_id") .setSmallIcon(R.drawable.notification_icon) // Replace with your app's drawable resource .setContentTitle("Example Notification") .setContentText("This is an example notification text.") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setContentIntent(pendingIntent) .setAutoCancel(true) // Dismisses the notification when clicked with(NotificationManagerCompat.from(this)) { notify(notificationId, builder.build()) }
In this example:
setSmallIcon
: This is the icon that appears in the notification. It's mandatory.setContentTitle
and setContentText
: These set the title and content of the notification.setPriority
: Sets the importance of the notification.setContentIntent
: This is the intent that will be launched when the user taps the notification.setAutoCancel
: If set to true
, the notification will be dismissed when it's tapped.Remember, there are many other features and options for notifications, such as adding actions, expanding detail views, and more. You can customize the notification as per your app's requirements using various methods provided by the NotificationCompat.Builder
class.
Creating notifications in Android:
// Create an explicit intent for the notification Intent intent = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); // Build the notification NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) .setContentTitle("My Notification") .setContentText("This is a simple notification.") .setContentIntent(pendingIntent) .setAutoCancel(true); // Show the notification NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); notificationManager.notify(notificationId, builder.build());
Custom notifications in Android example:
// Create a custom layout for the notification RemoteViews customLayout = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout); // Build the custom notification NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) .setContent(customLayout); // Show the custom notification NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); notificationManager.notify(notificationId, builder.build());
Foreground service with notifications in Android:
// Start the foreground service with a notification startForeground(NOTIFICATION_ID, notification); // Build the notification for the foreground service Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("Foreground Service") .setContentText("Service is running in the foreground.") .setSmallIcon(R.drawable.ic_notification) .build();
Notification styles in Android example:
// Use InboxStyle for a list of messages NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); inboxStyle.setBigContentTitle("Inbox Style Notification"); // Add messages to the InboxStyle inboxStyle.addLine("Message 1"); inboxStyle.addLine("Message 2"); // ... // Build the notification with InboxStyle NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) .setStyle(inboxStyle); // Show the notification NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); notificationManager.notify(notificationId, builder.build());
BigText style notification in Android:
// Use BigTextStyle for a expandable text notification NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle(); bigTextStyle.bigText("This is a long piece of text that will be displayed in expanded form."); // Build the notification with BigTextStyle NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) .setStyle(bigTextStyle); // Show the notification NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); notificationManager.notify(notificationId, builder.build());
Notification actions in Android example:
// Create an explicit intent for the notification action Intent actionIntent = new Intent(context, ActionReceiver.class); actionIntent.setAction("ACTION_NAME"); PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, actionIntent, 0); // Build the notification with actions NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) .setContentTitle("Notification with Actions") .addAction(R.drawable.ic_action1, "Action 1", actionPendingIntent) .addAction(R.drawable.ic_action2, "Action 2", actionPendingIntent); // Show the notification NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); notificationManager.notify(notificationId, builder.build());
Notification sound and vibration in Android:
// Set sound and vibration for the notification NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) .setContentTitle("Notification with Sound and Vibration") .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) .setVibration(new long[]{0, 1000, 500, 1000}); // Show the notification NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); notificationManager.notify(notificationId, builder.build());
Notification pending intent in Android example:
// Create a pending intent for a notification Intent intent = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); // Build the notification with a pending intent NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) .setContentTitle("Notification with Pending Intent") .setContentIntent(pendingIntent); // Show the notification NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); notificationManager.notify(notificationId, builder.build());