0
0
Android Kotlinmobile~20 mins

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

Choose your learning style9 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.