BCA / B.Tech 10 min read

Displaying Notifications in Hindi

Notifications दिखाना

Notification क्या है? (What is a Notification?)

Notification एक message होता है जो Android आपकी application के UI के बाहर दिखाता है ताकि user को reminders, updates, या दूसरी timely information दी जा सके।

जब user notification पर tap करता है, तो आमतौर पर आपकी app की कोई specific activity खुलती है। Notifications user के device की status bar में एक icon के रूप में दिखाई देते हैं।

Notifications क्यों महत्वपूर्ण हैं? (Why are Notifications Important?)

  • User Engagement: Users को app पर वापस लाने का एक अच्छा तरीका है।
  • Timely Information: Users को महत्वपूर्ण जानकारी तुरंत मिलती है, जैसे new message आना या download का complete होना।
  • Background Actions: App background में होने पर भी user को alert कर सकती है।

Notification के मुख्य हिस्से (Main Parts of a Notification)

[Image of an Android notification layout]
  • Small Icon (छोटा आइकन): Status bar में दिखाई देता है। यह अनिवार्य है।
  • App Name (ऐप का नाम): System द्वारा automatic दिखाया जाता है।
  • Timestamp (समय): System द्वारा automatic दिखाया जाता है।
  • Title (शीर्षक): Notification का मुख्य शीर्षक।
  • Text (टेक्स्ट): Notification का detailed message।

Notification Channels (Android 8.0 और उससे ऊपर)

Android 8.0 (API level 26) से, सभी notifications को एक channel को assign करना अनिवार्य है। Notification Channels users को आपकी app की different types की notifications को manage करने की सुविधा देते हैं।

  • उदाहरण: एक social media app में, "New Messages" के लिए एक channel और "Friend Requests" के लिए दूसरा channel हो सकता है। User चाहे तो "Friend Requests" channel को disable कर सकता है लेकिन "New Messages" को enabled रख सकता है।
  • आपको notification दिखाने से पहले channel register करना होगा। यह code app के start होने पर केवल एक बार चलना चाहिए।

Notification कैसे बनाएं (How to Create a Notification)

Notification बनाने की प्रक्रिया में कुछ steps शामिल हैं:

Step 1: Notification Channel बनाना (Create a Notification Channel)

यह code आपकी `MainActivity` के `onCreate()` में या `Application` class में रखा जा सकता है।


private void createNotificationChannel() {
    // Channel सिर्फ API 26+ पर बनाएं
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "MyChannelName";
        String description = "Channel for My App Notifications";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel("MY_CHANNEL_ID", name, importance);
        channel.setDescription(description);

        // System के साथ Channel को Register करें
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

Step 2: Notification बनाना और दिखाना (Build and Show the Notification)

यह code वहां लिखा जाता है जहाँ से आप notification trigger करना चाहते हैं (जैसे button click पर)।


// 1. NotificationCompat.Builder का उपयोग करके notification बनाएं
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MY_CHANNEL_ID")
        .setSmallIcon(R.drawable.ic_notification_icon) // छोटा icon set करें
        .setContentTitle("My Notification Title")      // Title set करें
        .setContentText("This is the notification message.") // Text set करें
        .setPriority(NotificationCompat.PRIORITY_DEFAULT); // Priority set करें

// 2. NotificationManagerCompat का instance प्राप्त करें
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

// 3. Notification दिखाएं
// notificationId हर notification के लिए unique होनी चाहिए
// यदि आप उसी id का उपयोग करते हैं, तो पुरानी notification update हो जाएगी
notificationManager.notify(1, builder.build());

Note: Notification पर click करने पर कोई activity खोलने के लिए, आपको एक `PendingIntent` बनाना होगा और उसे `.setContentIntent()` method का उपयोग करके notification builder में add करना होगा।