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 in Android Oreo (8+)

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.

Key Concepts:

  1. 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.

  2. 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.

How to Create and Use Notification Channels:

1. Create a Notification Channel:

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)
}

2. Send a Notification:

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.

User Control:

Users have full control over each notification channel. They can:

  • Change its importance.
  • Turn the notification channel on or off.
  • Change the notification sound.
  • Decide whether or not the notification should vibrate.
  • And much more.

Important Notes:

  • 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.

  1. Notifications in Android Oreo Example Code:

    • Description: Demonstrating the basic implementation of notifications in Android Oreo.
    • 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());
      
  2. Android Oreo Notification Group Example:

    • Description: Grouping notifications to organize and display them as a stack in the notification drawer.
    • Code:
      // 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());
      
  3. Notification Updates in Android Oreo:

    • Description: Updating and modifying existing notifications dynamically.
    • Code:
      // 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());