यह गाइड बताता है कि एंड्रॉइड में स्थायी (persistent) नोटिफिकेशन कैसे बनाए जाएँ जो उपयोगकर्ता के क्लिक करने या स्वाइप करने के बाद भी बने रहें। यह चल रहे कार्यों जैसे संगीत प्लेबैक, डाउनलोड, या नेविगेशन के लिए उपयोगी है।
मुख्य अवधारणा: setAutoCancel(false)
डिफ़ॉल्ट रूप से, नोटिफिकेशन पर टैप करने से वे खारिज हो जाते हैं। setAutoCancel(false) सेट करने से यह व्यवहार अक्षम हो जाता है, जिससे नोटिफिकेशन तब तक बना रहता है जब तक प्रोग्रामेटिक रूप से हटाया न जाए।
चरण 1: प्रोजेक्ट सेटअप
एंड्रॉइड स्टूडियो में "Empty Activity" के साथ एक नया प्रोजेक्ट बनाएँ।
चरण 2: लेआउट (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/btnCreateNotification"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="स्थायी नोटिफिकेशन दिखाएँ"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
चरण 3: MainActivity.java में लॉजिक
package com.example.persistentnotification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static final String CHANNEL_ID = "persistent_channel";
private static final String CHANNEL_NAME = "स्थायी नोटिफिकेशन";
private static int notificationId = 1001; // मैन्युअल कैंसिल के लिए निश्चित आईडी
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btnCreateNotification);
btn.setOnClickListener(v -> showPersistentNotification());
}
private void showPersistentNotification() {
createNotificationChannel();
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);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("स्थायी नोटिफिकेशन")
.setContentText("क्लिक या स्वाइप करने पर भी यह नहीं हटेगा")
.setOngoing(true) // "चल रहा" फ्लैग सेट करता है - स्वाइप से हटाने से रोकता है
.setAutoCancel(false) // टैप पर ऑटो-डिसमिस अक्षम करता है
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(notificationId, builder.build());
}
private void createNotificationChannel() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("क्लिक/स्वाइप के बाद भी बने रहने वाले नोटिफिकेशन");
channel.setLockscreenVisibility(android.app.Notification.VISIBILITY_PUBLIC);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
}
// वैकल्पिक: नोटिफिकेशन को प्रोग्रामेटिक रूप से क्लियर करने की विधि
public void clearPersistentNotification() {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(notificationId);
}
}
महत्वपूर्ण फ्लैग समझाए गए
| फ्लैग / विधि | उद्देश्य |
|---|---|
setOngoing(true) | नोटिफिकेशन को "चल रहे" इवेंट के रूप में चिह्नित करता है; उपयोगकर्ता द्वारा स्वाइप करके खारिज नहीं किया जा सकता |
setAutoCancel(false) | उपयोगकर्ता द्वारा टैप करने पर नोटिफिकेशन को स्वतः हटने से रोकता है |
| निश्चित notificationId | बाद में cancel(id) के माध्यम से प्रोग्रामेटिक निष्कासन सक्षम करता है |
मैनिफेस्ट (AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.persistentnotification">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PersistentNotification">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
उपयोग के मामले
- मीडिया प्लेबैक नियंत्रण
- फ़ाइल डाउनलोड/अपलोड प्रगति
- नेविगेशन दिशानिर्देश
- फोरग्राउंड सेवा संकेतक
नोट
Android 14+ (API 34) पर, फोरग्राउंड सेवाओं के लिए उचित foregroundServiceType मैनिफेस्ट में घोषित करना आवश्यक है। सुनिश्चित करें कि आपका ऐप नोटिफिकेशन अनुमति अनुरोधों को उचित तरीके से संभालता है।