नोटिफिकेशन अवलोकन
नोटिफिकेशन उपयोगकर्ता को ऐप के बाहर जानकारी दिखाने का तरीका है। Android 8.0 (API 26) से Notification Channels अनिवार्य हैं।
निर्भरता
// build.gradle (Module: app)
dependencies {
implementation 'androidx.core:core:1.12.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
// Material Design के लिए
implementation 'com.google.android.material:material:1.11.0'
}
बुनियादी नोटिफिकेशन (Android 8.0+)
1. नोटिफिकेशन चैनल बनाएं (एक बार, ऐप स्टार्ट पर)
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import androidx.core.app.NotificationCompat;
public class NotificationHelper {
private static final String CHANNEL_ID = "default_channel";
private static final String CHANNEL_NAME = "सामान्य सूचनाएं";
private static final String CHANNEL_DESC = "ऐप की सामान्य सूचनाएं";
public static void createNotificationChannel(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID,
CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT
);
channel.setDescription(CHANNEL_DESC);
channel.enableLights(true);
channel.setLightColor(Color.BLUE);
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{0, 500, 200, 500});
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = context.getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
}
}
// Application क्लास में कॉल करें:
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
NotificationHelper.createNotificationChannel(this);
}
}
2. नोटिफिकेशन बनाएं और दिखाएं
public void showBasicNotification(Context context, String title, String message) {
// इंटेंट: नोटिफिकेशन टैप पर क्या हो
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(
context, 0, intent,
PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT
);
// नोटिफिकेशन बिल्ड करें
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification) // आवश्यक: सफेद/पारदर्शी आइकन
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true) // टैप पर ऑटो-डिसमिस
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setDefaults(NotificationCompat.DEFAULT_ALL); // साउंड, वाइब्रेशन, लाइट
// दिखाएं
NotificationManagerCompat manager = NotificationManagerCompat.from(context);
manager.notify(1, builder.build()); // ID: 1 (अपडेट/कैंसिल के लिए)
}
उन्नत नोटिफिकेशन स्टाइल्स
बड़ा टेक्स्ट (BigTextStyle)
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("लंबा संदेश")
.setContentText("टैप करके पूरा पढ़ें...")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("यह एक बहुत लंबा संदेश है जो पूरी तरह से प्रदर्शित होगा जब उपयोगकर्ता नोटिफिकेशन को विस्तारित करेगा। " +
"यह कई लाइनों तक फैल सकता है और पूरी सामग्री दिखाता है।")
.setBigContentTitle("विस्तारित शीर्षक")
.setSummaryText("सारांश टेक्स्ट"))
.setPriority(NotificationCompat.PRIORITY_HIGH);
इमेज के साथ (BigPictureStyle)
Bitmap largeIcon = BitmapFactory.decodeResource(resources, R.drawable.large_image);
Bitmap bigPicture = BitmapFactory.decodeResource(resources, R.drawable.big_picture);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(largeIcon)
.setContentTitle("इमेज नोटिफिकेशन")
.setContentText("नीचे बड़ी इमेज देखें")
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bigPicture)
.bigLargeIcon(null) // विस्तारित होने पर लार्ज आइकन छुपाएं
.setSummaryText("इमेज कैप्शन"))
.setPriority(NotificationCompat.PRIORITY_HIGH);
इनबॉक्स स्टाइल (सूची)
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("नए संदेश")
.setContentText("3 नए संदेश")
.setStyle(new NotificationCompat.InboxStyle()
.addLine("राहुल: नमस्ते!")
.addLine("प्रिया: मीटिंग 3 बजे")
.addLine("अमित: फाइल भेज दी")
.setBigContentTitle("3 नए संदेश")
.setSummaryText("सभी देखें"))
.setPriority(NotificationCompat.PRIORITY_HIGH);
मीडिया स्टाइल (म्यूजिक प्लेयर)
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_music)
.setContentTitle("गाना शीर्षक")
.setContentText("कलाकार नाम")
.setLargeIcon(albumArt)
.setStyle(new NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 2) // कॉम्पैक्ट में दिखाएं: प्ले, पॉज, नेक्स्ट
.setMediaSession(mediaSession.getSessionToken()))
.addAction(R.drawable.ic_prev, "पिछला", prevPendingIntent)
.addAction(R.drawable.ic_play, "चलाएं", playPendingIntent)
.addAction(R.drawable.ic_next, "अगला", nextPendingIntent)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
एक्शन बटन्स जोड़ना
// रिप्लाई एक्शन (डायरेक्ट रिप्लाई - API 24+)
RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY)
.setLabel("उत्तर दें")
.build();
PendingIntent replyPendingIntent = PendingIntent.getBroadcast(
context, 0, new Intent(ACTION_REPLY),
PendingIntent.FLAG_IMMUTABLE
);
NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(
R.drawable.ic_reply, "उत्तर", replyPendingIntent)
.addRemoteInput(remoteInput)
.setAllowGeneratedReplies(true)
.build();
builder.addAction(replyAction);
// सामान्य एक्शन
builder.addAction(R.drawable.ic_open, "खोलें", openPendingIntent)
.addAction(R.drawable.ic_dismiss, "बाद में", dismissPendingIntent);
नोटिफिकेशन मैनेज करना
NotificationManagerCompat manager = NotificationManagerCompat.from(context); // अपडेट करें (समान ID, समान चैनल) manager.notify(notificationId, updatedBuilder.build()); // कैंसिल करें manager.cancel(notificationId); // सभी कैंसिल करें manager.cancelAll(); // बैज काउंट (लॉन्चर आइकन पर) manager.notify(notificationId, builder.setNumber(unreadCount).build());
फोरग्राउंड सर्विस नोटिफिकेशन
लंबे समय तक चलने वाले कार्यों (म्यूजिक, डाउनलोड, लोकेशन) के लिए:
// Service में
Notification notification = new NotificationCompat.Builder(this, FOREGROUND_CHANNEL_ID)
.setContentTitle("डाउनलोड हो रहा है...")
.setContentText("50% पूरा")
.setSmallIcon(R.drawable.ic_download)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setOngoing(true) // स्वाइप से डिसमिस नहीं होगा
.build();
startForeground(NOTIFICATION_ID, notification);
// बाद में अपडेट
notificationManager.notify(NOTIFICATION_ID, updatedNotification);
// रोकने पर
stopForeground(true); // true = नोटिफिकेशन भी हटाएं
सर्वोत्तम अभ्यास
- चैनल ID स्थिर रखें - रनटाइम में न बदलें
- स्मॉल आइकन सफेद/पारदर्शी बैकग्राउंड वाला वेक्टर ड्रॉएबल होना चाहिए
- प्राथमिकता उचित सेट करें: HIGH (हेड्स-अप), DEFAULT, LOW, MIN
- ग्रुपिंग
setGroup()के साथ संबंधित नोटिफिकेशन ग्रुप करें - श्रेणी
setCategory()सेट करें (MESSAGE, EMAIL, ALARM, आदि) - परीक्षण करें डू नॉट डिस्टर्ब, लॉकस्क्रीन, वियर OS पर
नोटिफिकेशन परमिशन (Android 13+)
// AndroidManifest.xml
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
// रनटाइम रिक्वेस्ट (Activity में)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS}, 1001);
}
}