Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
📱 Device Features  ·  Lesson 37

Notifications और Channels

Notification की बनावट

9:41▮▮ 85%⏰ Maths homeworkStudent Diary · nowExercise 5.2, questions 1–10Mark doneSnooze🔔 Physics revisionStudent Diary · 1h
Small icon, title, text, app name, time और action buttons
हिस्साBuilder method
Small icon (ज़रूरी)setSmallIcon()
Title / textsetContentTitle() / setContentText()
लंबा textsetStyle(BigTextStyle())
Tap actionsetContentIntent(pendingIntent)
ButtonsaddAction()

Step 1: Channel बनाइए

Kotlin
val channel = NotificationChannel(
    "reminders",                                  // id – never change after release
    "Reminders",                                  // name shown in Settings
    NotificationManager.IMPORTANCE_HIGH           // heads-up + sound
).apply { description = "Homework and exam reminders" }
getSystemService(NotificationManager::class.java).createNotificationChannel(channel)
Kotlin – StudentDiaryApp.kt (app start पर एक बार)
package com.codingeasily.studentdiary

import android.app.Application
import com.codingeasily.studentdiary.data.SettingsStore
import com.codingeasily.studentdiary.reminder.NotificationHelper

/**
 * Runs once when the app process starts (declared in AndroidManifest.xml).
 * We apply the saved light/dark theme and create the notification channel here.
 */
class StudentDiaryApp : Application() {
    override fun onCreate() {
        super.onCreate()
        SettingsStore(this).applyTheme()
        NotificationHelper.createChannel(this)
    }
}

Step 2: Permission (Android 13+)

Manifest में POST_NOTIFICATIONS declare करें और Android 13+ पर runtime पर माँगें (Runtime Permissions lesson देखें)। पुराने versions पर अपने आप मिलती है।

Step 3: Build करके दिखाइए

Kotlin – सबसे simple notification
val notification = NotificationCompat.Builder(this, "reminders")
    .setSmallIcon(R.drawable.ic_notification)     // white, transparent background
    .setContentTitle("⏰ Maths homework")
    .setContentText("Due tomorrow at 9:00 AM")
    .setPriority(NotificationCompat.PRIORITY_HIGH)
    .setAutoCancel(true)                          // remove after tap
    .build()

NotificationManagerCompat.from(this).notify(101, notification)   // 101 = id (same id replaces)

Student Diary का पूरा helper

Kotlin – reminder/NotificationHelper.kt
package com.codingeasily.studentdiary.reminder

import android.Manifest
import android.annotation.SuppressLint
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import com.codingeasily.studentdiary.MainActivity
import com.codingeasily.studentdiary.R

object NotificationHelper {

    const val CHANNEL_ID = "reminders"

    /** Android 8+ needs a channel before any notification can be shown. */
    fun createChannel(context: Context) {
        val channel = NotificationChannel(
            CHANNEL_ID,
            context.getString(R.string.channel_reminders),
            NotificationManager.IMPORTANCE_HIGH
        ).apply { description = context.getString(R.string.channel_reminders_desc) }
        context.getSystemService(NotificationManager::class.java).createNotificationChannel(channel)
    }

    fun canNotify(context: Context): Boolean =
        Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU ||
            ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) ==
            PackageManager.PERMISSION_GRANTED

    @SuppressLint("MissingPermission")          // we check canNotify() on the first line
    fun show(context: Context, noteId: Long, title: String, text: String) {
        if (!canNotify(context)) return

        // Tapping the notification opens the app
        val intent = Intent(context, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
        }
        val pending = PendingIntent.getActivity(
            context, noteId.toInt(), intent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )

        val notification = NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(title)
            .setContentText(text)
            .setStyle(NotificationCompat.BigTextStyle().bigText(text))
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setContentIntent(pending)
            .setAutoCancel(true)
            .build()

        try {
            NotificationManagerCompat.from(context).notify(noteId.toInt(), notification)
        } catch (e: SecurityException) {
            // permission was revoked while the app was running – ignore
        }
    }
}

Action buttons और progress

Kotlin
// "Mark done" button handled by a BroadcastReceiver
val doneIntent = Intent(this, MarkDoneReceiver::class.java).putExtra("noteId", note.id)
val donePending = PendingIntent.getBroadcast(this, note.id.toInt(), doneIntent,
    PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)

val builder = NotificationCompat.Builder(this, "reminders")
    .setSmallIcon(R.drawable.ic_notification)
    .setContentTitle("Maths homework")
    .addAction(R.drawable.ic_check, "Mark done", donePending)

// progress (e.g. uploading notes)
builder.setContentText("Uploading…").setProgress(100, 40, false)   // 40%
NotificationManagerCompat.from(this).notify(7, builder.build())

Reminder से notification तक

Note savedueAt = 9:00WorkManager9:00 तक delayReminderWorkerRoom से note पढ़ताNotificationHelpershow()User tapMainActivity खुलती
App बंद हो तब भी चलता है (अगला lesson)

आम गलतियाँ

  • Channel नहीं → Android 8+ पर कुछ नहीं दिखता।
  • Colourful या photo वाला small icon → सफ़ेद square दिखता है; simple white vector use करें।
  • FLAG_IMMUTABLE / FLAG_MUTABLE missing → Android 12+ पर crash।
  • अलग notes के लिए एक ही notification id → एक-दूसरे को replace करते हैं; note id use करें।

सारांश

  • Channel (एक बार) → permission (13+) → NotificationCompat.Builder → notify(id, …)।
  • Tap और action buttons के लिए FLAG_IMMUTABLE के साथ PendingIntent।
  • Code एक helper object में रखें और reuse करें।
✅ Practice

Settings में "Test notification" button जोड़िए जो BigText notification दिखाए, tap पर app खोले और "Mark done" action रखे।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

Android 8+ पर मेरा notification क्यों नहीं दिखता?
Android 8.0 (API 26) और ऊपर हर notification को channel चाहिए। Notifications दिखाने से पहले channel एक बार बनाइए (जैसे Application class में)।
क्या users एक तरह के notifications बंद कर सकते हैं?
हाँ। Users Settings में हर channel अलग से control करते हैं, इसलिए "Reminders" और "Offers" जैसे meaningful channels बनाइए।
PendingIntent क्या है?
एक token जो system को बाद में आपकी तरफ़ से Intent चलाने देता है — जैसे notification tap पर आपकी Activity खोलना। Android 12+ पर FLAG_IMMUTABLE use करें।
← Android Tutorial (Kotlin)
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicWorkManager, Services और Broadcasts आपकी प्रगति: 37 / 43 (86%) · Lesson 38 / 43 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें