📱 Device Features · Lesson 37
Notifications और Channels
Notification की बनावट
| हिस्सा | Builder method |
|---|---|
| Small icon (ज़रूरी) | setSmallIcon() |
| Title / text | setContentTitle() / setContentText() |
| लंबा text | setStyle(BigTextStyle()) |
| Tap action | setContentIntent(pendingIntent) |
| Buttons | addAction() |
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 तक
आम गलतियाँ
- Channel नहीं → Android 8+ पर कुछ नहीं दिखता।
- Colourful या photo वाला small icon → सफ़ेद square दिखता है; simple white vector use करें।
FLAG_IMMUTABLE/FLAG_MUTABLEmissing → 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 करें।
