Notification Channels पूरी गाइड
Android 8.0 (API 26) से नोटिफिकेशन चैनल अनिवार्य हैं। प्रत्येक चैनल का अपना महत्व, साउंड, वाइब्रेशन, लाइट सेटिंग्स होते हैं जिन्हें यूजर सिस्टम सेटिंग्स से कंट्रोल कर सकता है।
चरण 1-3: प्रोजेक्ट, लेआउट, साउंड फाइल (मानक)
चरण 4: MainActivity.java - चैनल बनाना और उपयोग
package app.tutorialspoint.com.notifyme;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public static final String CHANNEL_ID_HIGH = "channel_high";
public static final String CHANNEL_ID_LOW = "channel_low";
private static final String DEFAULT_CHANNEL_ID = "default";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNotificationChannels();
Button btnCreateNotification = findViewById(R.id.btnCreateNotification);
btnCreateNotification.setOnClickListener(v -> showNotificationWithChannel(CHANNEL_ID_HIGH));
}
private void createNotificationChannels() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return;
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// हाई इम्पोर्टेंस चैनल (साउंड, वाइब्रेशन, लाइट के साथ)
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
+ getPackageName() + "/raw/quite_impressed.mp3");
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationChannel highChannel = new NotificationChannel(
CHANNEL_ID_HIGH, "महत्वपूर्ण सूचनाएं", NotificationManager.IMPORTANCE_HIGH);
highChannel.setDescription("जरूरी अलर्ट और संदेश");
highChannel.enableLights(true);
highChannel.setLightColor(Color.RED);
highChannel.enableVibration(true);
highChannel.setVibrationPattern(new long[]{0, 500, 200, 500});
highChannel.setSound(sound, audioAttributes);
highChannel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
// लो इम्पोर्टेंस चैनल (साइलेंट, केवल बैज)
NotificationChannel lowChannel = new NotificationChannel(
CHANNEL_ID_LOW, "सामान्य सूचनाएं", NotificationManager.IMPORTANCE_LOW);
lowChannel.setDescription("प्रमोशन, अपडेट आदि");
lowChannel.enableLights(false);
lowChannel.enableVibration(false);
lowChannel.setSound(null, null);
lowChannel.setLockscreenVisibility(NotificationCompat.VISIBILITY_SECRET);
assert manager != null;
manager.createNotificationChannels(java.util.Arrays.asList(highChannel, lowChannel));
}
private void showNotificationWithChannel(String channelId) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(channelId.equals(CHANNEL_ID_HIGH) ? "महत्वपूर्ण!" : "सामान्य अपडेट")
.setContentText("यह " + channelId + " चैनल का नोटिफिकेशन है")
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.notify((int) System.currentTimeMillis(), builder.build());
}
}
चैनल प्रबंधन के उन्नत फीचर्स
चैनल ग्रुप्स (संबंधित चैनल्स को व्यवस्थित करने के लिए)
NotificationChannelGroup group = new NotificationChannelGroup("group_id", "संदेश");
manager.createNotificationChannelGroup(group);
channel.setGroup("group_id");
चैनल सेटिंग्स स्क्रीन खोलना
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName()); intent.putExtra(Settings.EXTRA_CHANNEL_ID, CHANNEL_ID_HIGH); startActivity(intent);
चैनल डिलीट/अपडेट करना
manager.deleteNotificationChannel(channelId)- चैनल हटाएं- मौजूदा चैनल की प्रॉपर्टीज (नाम, विवरण) अपडेट की जा सकती हैं, लेकिन महत्व (importance) नहीं बदला जा सकता।
Importance लेवल्स
| कॉन्स्टेंट | व्यवहार | उपयोग |
|---|---|---|
| IMPORTANCE_HIGH | साउंड + पीकिंग (हेड्स-अप) | कॉल, अलार्म, चैट संदेश |
| IMPORTANCE_DEFAULT | साउंड, नो पीकिंग | सामान्य नोटिफिकेशन |
| IMPORTANCE_LOW | नो साउंड, नो पीकिंग | प्रमोशन, बैकग्राउंड सिंक |
| IMPORTANCE_MIN | केवल शेड में, नो आइकन | डिबग, एनालिटिक्स |
| IMPORTANCE_NONE | ब्लॉक्ड | यूजर द्वारा ब्लॉक्ड |