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 हैं
- यूजर सेटिंग्स से बैज डिसेबल कर सकता है