Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Android ऐप में नोटिफिकेशन काउंट कैसे दिखाएं?

Android ऐप लॉन्चर आइकन पर नोटिफिकेशन बैज/काउंट दिखाना

यह ट्यूटोरियल दिखाता है कि कैसे नोटिफिकेशन भेजते समय बैज काउंट सेट किया जाता है, जिससे लॉन्चर आइकन पर अनरीड काउंट दिखाई देता है।

चरण 1: प्रोजेक्ट बनाएं

Android Studio में नया प्रोजेक्ट बनाएं।

चरण 2: लेआउट

res/layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">
    <Button
        android:id="@+id/btnCreateNotification"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="नोटिफिकेशन बनाएं" />
</RelativeLayout>

चरण 3: नोटिफिकेशन चैनल और बैज (API 26+)

src/MainActivity.java:

package com.app.sample;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.annotation.SuppressLint;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private static int notificationCount = 0;
    public static final String CHANNEL_ID = "notification_channel";
    private static final String CHANNEL_NAME = "नोटिफिकेशन चैनल";
    private NotificationManagerCompat notificationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        notificationManager = NotificationManagerCompat.from(this);
        createNotificationChannel();
        
        findViewById(R.id.btnCreateNotification).setOnClickListener(v -> createNotification());
    }

    @Override
    protected void onResume() {
        super.onResume();
        // ऐप खुलने पर काउंट रीसेट करें (वैकल्पिक)
        notificationCount = 0;
        updateBadge(0);
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            channel.setDescription("ऐप नोटिफिकेशन्स");
            channel.setShowBadge(true); // बैज इनेबल करें
            notificationManager.createNotificationChannel(channel);
        }
    }

    @SuppressLint("MissingPermission")
    private void createNotification() {
        notificationCount++;
        
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
            PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
        
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentTitle("मेरी नोटिफिकेशन")
            .setContentText("नोटिफिकेशन #" + notificationCount)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setNumber(notificationCount) // बैज काउंट सेट करें
            .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL);
        
        // यूनिक ID के लिए टाइमस्टैम्प उपयोग करें
        int notificationId = (int) System.currentTimeMillis();
        notificationManager.notify(notificationId, builder.build());
        
        // लॉन्चर बैज अपडेट करें (कुछ लॉन्चर्स के लिए)
        updateBadge(notificationCount);
    }

    private void updateBadge(int count) {
        // कुछ लॉन्चर्स शॉर्टकट बैजिंग सपोर्ट करते हैं
        // इसके लिए अतिरिक्त लाइब्रेरी या लॉन्चर-स्पेसिफिक API चाहिए
        // उदाहरण: Nova Launcher, Sony, Samsung, HTC लॉन्चर्स
    }
}

बैज काउंट के प्रकार

तरीकाकहां दिखता हैसपोर्ट
Notification.setNumber()नोटिफिकेशन शेड + कुछ लॉन्चर बैजAPI 4+
NotificationChannel.setShowBadge()लॉन्चर आइकन बैज (डॉट/नंबर)API 26+
ShortcutBadger लाइब्रेरीअधिकांश कस्टम लॉन्चर्सथर्ड-पार्टी
Notification Badge (Android 8+)ऐप आइकन पर डॉटसिस्टम-लेवल

थर्ड-पार्टी लाइब्रेरी: ShortcutBadger

// build.gradle
implementation "me.leolin:ShortcutBadger:1.1.22@aar"

// उपयोग
ShortcutBadger.applyCount(context, count); // बैज सेट करें
ShortcutBadger.removeCount(context); // बैज हटाएं

नोटिफिकेशन चैनल कस्टमाइजेशन

NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, 
    NotificationManager.IMPORTANCE_HIGH);
channel.enableLights(true);
channel.setLightColor(Color.RED);
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{100, 200, 300});
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
channel.setShowBadge(true); // महत्वपूर्ण: बैज इनेबल करें

चरण 4: मैनिफेस्ट

<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.app.sample">
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> <!-- API 33+ -->
    <application ...>
        <activity android:name=".MainActivity">...</activity>
    </application>
</manifest>

रनटाइम परमिशन (Android 13+)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) 
        != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, 
            new String[]{Manifest.permission.POST_NOTIFICATIONS}, 101);
    }
}

बेस्ट प्रैक्टिसेज

  • नोटिफिकेशन चैनल बनाएं (API 26+ अनिवार्य)
  • setNumber() सेट करें नोटिफिकेशन पर
  • चैनल में setShowBadge(true) सेट करें
  • ऐप खुलने पर काउंट रीसेट करें (onResume में)
  • नोटिफिकेशन ID यूनिक रखें (System.currentTimeMillis())
  • बड़े ऐप्स के लिए WorkManager से पीरियॉडिक सिंक करें

सीमाएं

  • सभी लॉन्चर्स बैज सपोर्ट नहीं करते
  • स्टॉक Android (Pixel) केवल डॉट दिखाता है, नंबर नहीं
  • कुछ OEMs (Samsung, Xiaomi, Huawei) के अपने API हैं
  • यूजर सेटिंग्स से बैज डिसेबल कर सकता है
  1. एंड्रॉइड ऐप में TextToSpeech कैसे बनाएं? एंड्रॉइड ऐप में TextToSpeech कैसे बनाएं?

    एंड्रॉइड ऐप में टेक्स्ट-टू-स्पीच (TTS) लागू करना यह उदाहरण दिखाता है कि एंड्रॉइड ऐप में TextToSpeech फीचर कैसे जोड़ा जाता है, जिससे ऐप लिखे गए टेक्स्ट को आवाज़ में बदल सके। चरण 1: नया प्रोजेक्ट बनाएं Android Studio खोलें, File → New Project चुनें और आवश्यक विवरण भरकर नया प्रोजेक्ट बनाएं। चरण 2: ले

  1. Facebook पर Android ऐप कैसे बनाएं? Facebook पर Android ऐप कैसे बनाएं?

    Facebook Developer Console पर ऐप बनाकर App ID प्राप्त करने की चरण-दर-चरण प्रक्रिया। चरण 1: Facebook Developer Console पर ऐप बनाएं developers.facebook.com पर जाएं My Apps → Create App चुनें ऐप नाम और ईमेल दर्ज करें Create App ID पर क्लिक करें चरण 2: Android Studio में SDK जोड़ें प्रोजेक्ट-लेवल bu

  1. एंड्रॉइड 8 स्नूज़ मास्टर करें: बेहतर नींद के लिए नोटिफिकेशन प्रबंधित करें एंड्रॉइड 8 स्नूज़ मास्टर करें: बेहतर नींद के लिए नोटिफिकेशन प्रबंधित करें

    हम में से ज्यादातर के लिए बिस्तर पर जाने से पहले दिन का आखिरी काम आने वाले दिन के लिए अलार्म सेट करना होता है। हालांकि, हम में से ज्यादातर आरामदायक बिस्तर के दायरे से खुद को बाहर खींचने से पहले कम से कम कुछ बार स्नूज़ बटन दबाते हैं। आश्चर्यजनक रूप से, ये दो स्नूज़ के बीच के कुछ मिनट हैं जब हमें सबसे