flutterjunction.com
🔔
Android Notifications
A Guide to User-Facing Alerts in Android
flutterjunction.com
Notification
● Communicate with users outside your app’s UI
● Display reminders, updates, messages, or ongoing tasks
● Critical for user re-engagement
● Users can interact with notifications to open the app or perform actions.
flutterjunction.com
Usage of Notification
● Engage users even when the app is not running.(e.g chat notification)
● Provide real-time updates or alerts. (e.g file download)
● Improve user experience by offering quick actions.
flutterjunction.com
Permission Handling (Android 13+)
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
boolean notificationsEnabled = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
notificationsEnabled = notificationManager.areNotificationsEnabled();
} else {
// Pre-Android 13 always has notification permission
notificationsEnabled = true;
}
flutterjunction.com
Request Permission Dynamically
Check Android Version
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
// Only request permission on Android 13+
}
Check if Permission is Already Granted
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
!= PackageManager.PERMISSION_GRANTED) { // Permission not granted, request it
}
flutterjunction.com
Request Notification Dynamically
Request Notification Permission
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.POST_NOTIFICATIONS},
101 // Request code
);
flutterjunction.com
Request Notification Dynamically
Handle User Response
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 101) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted — show notification
} else { // Permission denied — inform user
}}
}
flutterjunction.com
Notification Components
📢 Notification Channel (Android 8.0+)
● Categorizes notifications by type and importance level
● Required for managing sound, vibration, and visibility settings
● Users can control settings per channel (sound, vibration, etc.)
● Required to group and manage notifications.
️
🛠️Notification Builder (NotificationCompat.Builder)
● Constructs the layout and content of the notification
● Sets title, text, icons, actions, etc.
flutterjunction.com
Notification Components
🎯 PendingIntent
● Defines what should happen when the user taps the notification
● Commonly used to launch an activity or service
📬 Notification Manager (NotificationManager)
● System service responsible for sending and displaying the notification
● Uses notify(id, notification) to post it to the status bar
flutterjunction.com
Create a Notification Channel (Android 8.0+)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"channel_id", // Unique ID
"General Notifications", // Channel Name
NotificationManager.IMPORTANCE_DEFAULT // Importance Level
);
channel.setDescription("This channel is used for general notifications.");
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
flutterjunction.com
Importance Level
● IMPORTANCE_MIN (No sound/vibration)
● IMPORTANCE_DEFAULT (Sound, no pop-up)
● IMPORTANCE_HIGH (Sound + Pop-up)
● IMPORTANCE_MAX (Critical/Urgent alerts)
flutterjunction.com
Create a PendingIntent
Intent intent = new Intent(this, YourTargetActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);
flutterjunction.com
Build the Notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,
"channel_id")
.setSmallIcon(R.drawable.ic_notification) // Must have
.setContentTitle("New Alert!")
.setContentText("You have a new message.")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent) // Trigger when tapped
.setAutoCancel(true); // Dismiss when clicked
flutterjunction.com
Show the Notification
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(1001, builder.build()); // 1001 is the notification ID
flutterjunction.com
Types of Notifications
Basic Notification
● Contains a title, content text, and a small icon.
● Appears in the status bar and notification panel.
● Tapping the notification opens the app or a specific screen.
● E.g “📩 New Message: You have a new message from Hari bahadur.”
Expandable Notification
● Shows additional content when the user swipes down.
● Useful for displaying longer messages, images, or multiple lines.
● E.g Detailed messages, articles
flutterjunction.com
Types of Notifications
Action Notification
● Includes action buttons that allow users to perform tasks directly from
the notification.
● Common for Reply, Archive, Like, etc.
flutterjunction.com
Key Points to Remember
● 📲 Notifications keep users engaged even when the app is not running.
● 🧱 Main components: Notification Channel, Builder, PendingIntent, and
Notification Manager.
● 🔔 Types: Basic, Expandable, and Action-based notifications.
● 🔐 From Android 13+, runtime permission (POST_NOTIFICATIONS) is required.
● 🎯 Use PendingIntent to define custom actions when a user taps a notification.
● 📡 Can be triggered by background services, alarms, or broadcast receivers.
● ✨ Design user-friendly, non-intrusive, and meaningful notifications to improve
user experience.
flutterjunction.com
Thank you!!!
flutterjunction.com

Android Notifications-A Guide to User-Facing Alerts in Android .pptx

  • 1.
  • 2.
    flutterjunction.com Notification ● Communicate withusers outside your app’s UI ● Display reminders, updates, messages, or ongoing tasks ● Critical for user re-engagement ● Users can interact with notifications to open the app or perform actions.
  • 3.
    flutterjunction.com Usage of Notification ●Engage users even when the app is not running.(e.g chat notification) ● Provide real-time updates or alerts. (e.g file download) ● Improve user experience by offering quick actions.
  • 4.
    flutterjunction.com Permission Handling (Android13+) NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); boolean notificationsEnabled = false; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { notificationsEnabled = notificationManager.areNotificationsEnabled(); } else { // Pre-Android 13 always has notification permission notificationsEnabled = true; }
  • 5.
    flutterjunction.com Request Permission Dynamically CheckAndroid Version if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { // Only request permission on Android 13+ } Check if Permission is Already Granted if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { // Permission not granted, request it }
  • 6.
    flutterjunction.com Request Notification Dynamically RequestNotification Permission ActivityCompat.requestPermissions( this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, 101 // Request code );
  • 7.
    flutterjunction.com Request Notification Dynamically HandleUser Response @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == 101) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Permission granted — show notification } else { // Permission denied — inform user }} }
  • 8.
    flutterjunction.com Notification Components 📢 NotificationChannel (Android 8.0+) ● Categorizes notifications by type and importance level ● Required for managing sound, vibration, and visibility settings ● Users can control settings per channel (sound, vibration, etc.) ● Required to group and manage notifications. ️ 🛠️Notification Builder (NotificationCompat.Builder) ● Constructs the layout and content of the notification ● Sets title, text, icons, actions, etc.
  • 9.
    flutterjunction.com Notification Components 🎯 PendingIntent ●Defines what should happen when the user taps the notification ● Commonly used to launch an activity or service 📬 Notification Manager (NotificationManager) ● System service responsible for sending and displaying the notification ● Uses notify(id, notification) to post it to the status bar
  • 10.
    flutterjunction.com Create a NotificationChannel (Android 8.0+) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( "channel_id", // Unique ID "General Notifications", // Channel Name NotificationManager.IMPORTANCE_DEFAULT // Importance Level ); channel.setDescription("This channel is used for general notifications."); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(channel); }
  • 11.
    flutterjunction.com Importance Level ● IMPORTANCE_MIN(No sound/vibration) ● IMPORTANCE_DEFAULT (Sound, no pop-up) ● IMPORTANCE_HIGH (Sound + Pop-up) ● IMPORTANCE_MAX (Critical/Urgent alerts)
  • 12.
    flutterjunction.com Create a PendingIntent Intentintent = new Intent(this, YourTargetActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent pendingIntent = PendingIntent.getActivity( this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE );
  • 13.
    flutterjunction.com Build the Notification NotificationCompat.Builderbuilder = new NotificationCompat.Builder(this, "channel_id") .setSmallIcon(R.drawable.ic_notification) // Must have .setContentTitle("New Alert!") .setContentText("You have a new message.") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setContentIntent(pendingIntent) // Trigger when tapped .setAutoCancel(true); // Dismiss when clicked
  • 14.
    flutterjunction.com Show the Notification NotificationManagerCompatnotificationManager = NotificationManagerCompat.from(this); notificationManager.notify(1001, builder.build()); // 1001 is the notification ID
  • 15.
    flutterjunction.com Types of Notifications BasicNotification ● Contains a title, content text, and a small icon. ● Appears in the status bar and notification panel. ● Tapping the notification opens the app or a specific screen. ● E.g “📩 New Message: You have a new message from Hari bahadur.” Expandable Notification ● Shows additional content when the user swipes down. ● Useful for displaying longer messages, images, or multiple lines. ● E.g Detailed messages, articles
  • 16.
    flutterjunction.com Types of Notifications ActionNotification ● Includes action buttons that allow users to perform tasks directly from the notification. ● Common for Reply, Archive, Like, etc.
  • 17.
    flutterjunction.com Key Points toRemember ● 📲 Notifications keep users engaged even when the app is not running. ● 🧱 Main components: Notification Channel, Builder, PendingIntent, and Notification Manager. ● 🔔 Types: Basic, Expandable, and Action-based notifications. ● 🔐 From Android 13+, runtime permission (POST_NOTIFICATIONS) is required. ● 🎯 Use PendingIntent to define custom actions when a user taps a notification. ● 📡 Can be triggered by background services, alarms, or broadcast receivers. ● ✨ Design user-friendly, non-intrusive, and meaningful notifications to improve user experience.
  • 18.