Bird
Raised Fist0
Android Kotlinmobile~20 mins

Notification channels in Android Kotlin - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Notification Channel Demo
This screen demonstrates creating a notification channel and sending a notification using that channel.
Target UI
-------------------------
| Notification Channel   |
| Demo Screen            |
|                       |
| [Send Notification]   |
|                       |
-------------------------
Create a notification channel with a specific ID, name, and importance.
Add a button labeled 'Send Notification'.
When the button is tapped, send a notification using the created channel.
Notification should have a title and a short message.
Ensure the notification channel is created only once.
Starter Code
Android Kotlin
package com.example.notificationdemo

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button

class MainActivity : AppCompatActivity() {
    private val CHANNEL_ID = "demo_channel"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // TODO: Create notification channel here

        val sendButton: Button = findViewById(R.id.sendNotificationButton)
        sendButton.setOnClickListener {
            // TODO: Send notification here
        }
    }
}
Task 1
Task 2
Solution
Android Kotlin
package com.example.notificationdemo

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat

class MainActivity : AppCompatActivity() {
    private val CHANNEL_ID = "demo_channel"
    private val NOTIFICATION_ID = 101

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        createNotificationChannel()

        val sendButton: Button = findViewById(R.id.sendNotificationButton)
        sendButton.setOnClickListener {
            sendNotification()
        }
    }

    private fun createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val name = "Demo Channel"
            val descriptionText = "Channel for demo notifications"
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
                description = descriptionText
            }
            val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }

    private fun sendNotification() {
        val builder = NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(android.R.drawable.ic_dialog_info)
            .setContentTitle("Demo Notification")
            .setContentText("This is a notification sent using a channel.")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)

        with(NotificationManagerCompat.from(this)) {
            notify(NOTIFICATION_ID, builder.build())
        }
    }
}

We first create a notification channel only if the Android version is Oreo or higher, because channels are supported from API 26 onwards. The channel has an ID, a user-visible name, and importance level.

We then set up a button click listener that sends a notification using NotificationCompat.Builder with the channel ID. This ensures the notification uses the created channel.

The notification has a small icon, a title, and a short message. We use NotificationManagerCompat to show the notification.

Final Result
Completed Screen
-------------------------
| Notification Channel   |
| Demo Screen            |
|                       |
| [Send Notification]   |
|                       |
-------------------------
User taps 'Send Notification' button.
A notification appears in the device's notification shade with title 'Demo Notification' and message 'This is a notification sent using a channel.'
Stretch Goal
Add a second notification channel with high importance and send a notification using it.
💡 Hint
Create another NotificationChannel with IMPORTANCE_HIGH and use its ID in NotificationCompat.Builder.

Practice

(1/5)
1. What is the main purpose of a notification channel in Android apps?
easy
A. To store notification data in a database
B. To send notifications without user permission
C. To create animations for notifications
D. To group notifications and let users control their behavior

Solution

  1. Step 1: Understand notification channels

    Notification channels group notifications by type and allow users to control settings like sound and vibration.
  2. Step 2: Identify the correct purpose

    Only To group notifications and let users control their behavior describes grouping and user control, which is the main purpose of notification channels.
  3. Final Answer:

    To group notifications and let users control their behavior -> Option D
  4. Quick Check:

    Notification channels = Group and control notifications [OK]
Hint: Channels group notifications for user control [OK]
Common Mistakes:
  • Thinking channels send notifications themselves
  • Confusing channels with notification content
  • Assuming channels store data
2. Which of the following is the correct way to create a notification channel in Kotlin?
easy
A. val channel = NotificationChannel("id", NotificationManager.IMPORTANCE_HIGH, "name")
B. val channel = NotificationChannel("name", "id", NotificationManager.IMPORTANCE_HIGH)
C. val channel = NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH)
D. val channel = NotificationChannel(NotificationManager.IMPORTANCE_HIGH, "id", "name")

Solution

  1. Step 1: Check NotificationChannel constructor parameters

    The constructor requires (id: String, name: CharSequence, importance: Int) in that order.
  2. Step 2: Match parameters with options

    Only val channel = NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH) matches the correct order: id, name, importance.
  3. Final Answer:

    val channel = NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH) -> Option C
  4. Quick Check:

    Constructor order = id, name, importance [OK]
Hint: Remember constructor order: id, name, importance [OK]
Common Mistakes:
  • Swapping id and name parameters
  • Putting importance before name
  • Using wrong parameter types
3. Given this Kotlin code snippet, what will happen when it runs on Android 8.0+?
val channel = NotificationChannel("news", "News Updates", NotificationManager.IMPORTANCE_DEFAULT)
val manager = getSystemService(NotificationManager::class.java)
manager.createNotificationChannel(channel)
medium
A. A new notification channel named "News Updates" is created with default importance
B. The app crashes because the channel ID is invalid
C. No channel is created because createNotificationChannel is deprecated
D. The notification channel is created but with low importance

Solution

  1. Step 1: Analyze the code creating the channel

    The code creates a NotificationChannel with ID "news", name "News Updates", and importance DEFAULT.
  2. Step 2: Understand Android 8.0+ behavior

    On Android 8.0 and above, this code creates the channel successfully with the given importance.
  3. Final Answer:

    A new notification channel named "News Updates" is created with default importance -> Option A
  4. Quick Check:

    createNotificationChannel creates channel with given importance [OK]
Hint: createNotificationChannel creates channel with given importance [OK]
Common Mistakes:
  • Thinking channel ID must be a number
  • Believing createNotificationChannel is deprecated
  • Confusing importance levels
4. What is wrong with this Kotlin code that tries to create a notification channel?
val channel = NotificationChannel("alerts", "Alerts", 5)
val manager = getSystemService(NotificationManager::class.java)
manager.createNotificationChannel(channel)
medium
A. NotificationChannel constructor requires four parameters
B. The importance level 5 is invalid; use predefined constants instead
C. getSystemService cannot be used to get NotificationManager
D. The channel ID "alerts" must be an integer

Solution

  1. Step 1: Check importance parameter

    Importance must be one of NotificationManager.IMPORTANCE_* constants, not arbitrary integers like 5.
  2. Step 2: Verify other parameters

    Channel ID can be a string, getSystemService usage is correct, and constructor takes three parameters.
  3. Final Answer:

    The importance level 5 is invalid; use predefined constants instead -> Option B
  4. Quick Check:

    Importance must be NotificationManager constants [OK]
Hint: Use NotificationManager.IMPORTANCE_* constants for importance [OK]
Common Mistakes:
  • Passing raw integers for importance
  • Thinking channel ID must be numeric
  • Misusing getSystemService method
5. You want to create two notification channels: one for "Messages" with high importance and one for "Promotions" with low importance. Which Kotlin code correctly creates both channels and registers them with the system?
hard
A. val msgChannel = NotificationChannel("msg", "Messages", NotificationManager.IMPORTANCE_HIGH) val promoChannel = NotificationChannel("promo", "Promotions", NotificationManager.IMPORTANCE_LOW) val manager = getSystemService(NotificationManager::class.java) manager.createNotificationChannel(msgChannel) manager.createNotificationChannel(promoChannel)
B. val msgChannel = NotificationChannel("Messages", "msg", NotificationManager.IMPORTANCE_HIGH) val promoChannel = NotificationChannel("Promotions", "promo", NotificationManager.IMPORTANCE_LOW) val manager = getSystemService(NotificationManager::class.java) manager.createNotificationChannel(msgChannel) manager.createNotificationChannel(promoChannel)
C. val msgChannel = NotificationChannel("msg", "Messages", 1) val promoChannel = NotificationChannel("promo", "Promotions", 3) val manager = getSystemService(NotificationManager::class.java) manager.createNotificationChannel(msgChannel) manager.createNotificationChannel(promoChannel)
D. val msgChannel = NotificationChannel("msg", "Messages", NotificationManager.IMPORTANCE_LOW) val promoChannel = NotificationChannel("promo", "Promotions", NotificationManager.IMPORTANCE_HIGH) val manager = getSystemService(NotificationManager::class.java) manager.createNotificationChannel(msgChannel) manager.createNotificationChannel(promoChannel)

Solution

  1. Step 1: Verify channel IDs and names

    Channel ID should be a unique string identifier, and name is a user-visible string. val msgChannel = NotificationChannel("msg", "Messages", NotificationManager.IMPORTANCE_HIGH) val promoChannel = NotificationChannel("promo", "Promotions", NotificationManager.IMPORTANCE_LOW) val manager = getSystemService(NotificationManager::class.java) manager.createNotificationChannel(msgChannel) manager.createNotificationChannel(promoChannel) uses correct order and values.
  2. Step 2: Check importance levels

    Messages channel uses IMPORTANCE_HIGH, Promotions uses IMPORTANCE_LOW as required. val msgChannel = NotificationChannel("msg", "Messages", NotificationManager.IMPORTANCE_HIGH) val promoChannel = NotificationChannel("promo", "Promotions", NotificationManager.IMPORTANCE_LOW) val manager = getSystemService(NotificationManager::class.java) manager.createNotificationChannel(msgChannel) manager.createNotificationChannel(promoChannel) matches this.
  3. Step 3: Confirm channel creation calls

    Both channels are created via createNotificationChannel calls on NotificationManager instance.
  4. Final Answer:

    Option A correctly creates and registers both channels with proper IDs, names, and importance levels -> Option A
  5. Quick Check:

    Correct IDs, names, importance, and registration = val msgChannel = NotificationChannel("msg", "Messages", NotificationManager.IMPORTANCE_HIGH) val promoChannel = NotificationChannel("promo", "Promotions", NotificationManager.IMPORTANCE_LOW) val manager = getSystemService(NotificationManager::class.java) manager.createNotificationChannel(msgChannel) manager.createNotificationChannel(promoChannel) [OK]
Hint: Use correct ID, name, importance, then call createNotificationChannel twice [OK]
Common Mistakes:
  • Swapping ID and name parameters
  • Using raw integers for importance
  • Mixing importance levels between channels
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard