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

एंड्रॉइड में नोटिफिकेशन इंटेंट को स्वयं कैसे साफ़ करें?

यह ट्यूटोरियल दर्शाता है कि एक नोटिफिकेशन कैसे बनाया जाए जो **उपयोगकर्ता इंटरैक्शन पर स्वयं साफ़ हो जाए** - या तो टैप करने पर (डिफ़ॉल्ट) या एक्शन बटन के माध्यम से। यह मानक `setAutoCancel(true)` और कस्टम खारिज करने वाले PendingIntent दोनों को कवर करता है।

दृष्टिकोण 1: सरल ऑटो-कैंसिल (टैप पर)

सबसे आम पैटर्न: उपयोगकर्ता टैप करता है → नोटिफिकेशन गायब हो जाता है → एक्टिविटी खुलती है।

MainActivity.java

package com.example.selfclearingnotification;

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 = "self_clear_channel";
    private static final String CHANNEL_NAME = "सेल्फ-क्लियर नोटिफिकेशन";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = findViewById(R.id.btnCreateNotification);
        btn.setOnClickListener(v -> createSelfClearingNotification());
    }

    private void createSelfClearingNotification() {
        createNotificationChannel();

        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("from_notification", true);

        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("मुझे टैप करें - मैं स्वयं गायब हो जाऊँगा")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);  // ← मुख्य: टैप पर स्वतः खारिज

        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        int id = (int) System.currentTimeMillis();
        manager.notify(id, 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_DEFAULT);
            channel.setDescription("टैप करने पर स्वयं साफ़ होने वाले नोटिफिकेशन");
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);
        }
    }
}

दृष्टिकोण 2: कस्टम एक्शन बटन के साथ खारिज (पैनल बंद करता है)

"खारिज करें" बटन जो नोटिफिकेशन पैनल को बंद करता है *बिना* मुख्य एक्टिविटी खोले।

DismissReceiver.java

package com.example.selfclearingnotification;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class DismissReceiver extends BroadcastReceiver {

    public static final String ACTION_DISMISS = "com.example.ACTION_DISMISS";
    public static final String EXTRA_NOTIFICATION_ID = "notification_id";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (ACTION_DISMISS.equals(intent.getAction())) {
            int notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1);
            
            // 1. नोटिफिकेशन पैनल/शेड बंद करें
            Intent closeDialogs = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            context.sendBroadcast(closeDialogs);

            // 2. नोटिफिकेशन कैंसिल करें
            if (notificationId != -1) {
                NotificationManager manager = 
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                manager.cancel(notificationId);
            }
        }
    }
}

MainActivity.java (एक्शन बटन के साथ अद्यतन)

// ... imports ...

public class MainActivity extends AppCompatActivity {
    // ... onCreate, createNotificationChannel समान ...

    private void createNotificationWithDismissButton() {
        createNotificationChannel();
        int notificationId = (int) System.currentTimeMillis();

        // खारिज एक्शन PendingIntent
        Intent dismissIntent = new Intent(this, DismissReceiver.class);
        dismissIntent.setAction(DismissReceiver.ACTION_DISMISS);
        dismissIntent.putExtra(DismissReceiver.EXTRA_NOTIFICATION_ID, notificationId);
        PendingIntent dismissPendingIntent = PendingIntent.getBroadcast(
            this, 0, dismissIntent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);

        // मुख्य सामग्री PendingIntent
        Intent contentIntent = new Intent(this, MainActivity.class);
        contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent contentPendingIntent = PendingIntent.getActivity(
            this, 1, contentIntent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentTitle("कस्टम खारिज बटन")
            .setContentText("पैनल बंद करने के लिए 'खारिज करें' दबाएँ")
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setContentIntent(contentPendingIntent)
            .addAction(R.drawable.ic_close, "खारिज करें", dismissPendingIntent)  // एक्शन बटन
            .setAutoCancel(true); // मुख्य टैप पर भी खारिज करता है

        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(notificationId, builder.build());
    }
}

AndroidManifest.xml - रिसीवर पंजीकृत करें

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.selfclearingnotification">

    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

    <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.SelfClearingNotification">

        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".DismissReceiver"
            android:exported="false">
        </receiver>
    </application>
</manifest>

दृष्टिकोण 3: समय-आधारित ऑटो-कैंसिल (हैंडलर)

उपयोगकर्ता इंटरैक्शन के बिना निर्धारित समय के बाद नोटिफिकेशन साफ़ करता है।

// नोटिफिकेशन दिखाने के बाद:
int notificationId = (int) System.currentTimeMillis();
manager.notify(notificationId, builder.build());

// 10 सेकंड बाद ऑटो-कैंसिल शेड्यूल करें
new Handler(Looper.getMainLooper()).postDelayed(() -> {
    manager.cancel(notificationId);
}, 10_000); // 10 सेकंड

तुलना तालिका

विधिट्रिगरपैनल बंद करता है?उपयोग का मामला
setAutoCancel(true)उपयोगकर्ता मुख्य सामग्री पर टैप करता हैहाँमानक नोटिफिकेशन
BroadcastReceiver + ACTION_CLOSE_SYSTEM_DIALOGSउपयोगकर्ता एक्शन बटन दबाता हैहाँ"स्नूज़", "खारिज करें", "बाद में याद दिलाएँ"
Handler.postDelayed + cancel()समय समाप्तिहाँ (यदि दिख रहा हो)अस्थायी अलर्ट, टोस्ट-जैसे नोटिफिकेशन
setTimeoutAfter() (API 26+)समय समाप्तिहाँआधुनिक API, हैंडलर से सरल

आधुनिक API: setTimeoutAfter() (Android 8.0+)

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
    // ...
    .setTimeoutAfter(10_000); // 10 सेकंड बाद स्वतः कैंसिल, कोई हैंडलर आवश्यक नहीं

सर्वोत्तम अभ्यास

  1. डिफ़ॉल्ट के लिए setAutoCancel(true) उपयोग करें - यह वही है जो उपयोगकर्ता अपेक्षा करते हैं
  2. एक्शन बटन के लिए BroadcastReceiver उपयोग करें - UI लॉन्च किए बिना पृष्ठभूमि में संभालता है
  3. ACTION_CLOSE_SYSTEM_DIALOGS भेजें - एक्शन बटन दबाए जाने पर पैनल बंद करता है
  4. अद्वितीय notificationId बनाए रखें - सटीक कैंसिलेशन सक्षम करता है
  5. exported="false" आंतरिक रिसीवर के लिए
  1. एंड्रॉइड में नोटिफिकेशन कैसे शेड्यूल करें? एंड्रॉइड में नोटिफिकेशन कैसे शेड्यूल करें?

    यह व्यापक गाइड एंड्रॉइड में नोटिफिकेशन शेड्यूल करने के **तीन प्रमुख तरीकों** को कवर करता है: WorkManager (आधुनिक मानक), AlarmManager (सटीक समय), और Handler (सरल अल्पकालिक विलंब)। त्वरित निर्णय मार्गदर्शिका आवश्यकताअनुशंसित दृष्टिकोण विश्वसनीय, बैटरी-कुशल, लचीला समयWorkManager सटीक सेकंड/मिनट डि

  1. एंड्रॉइड ऐप के लिए कस्टम पुश नोटिफिकेशन कैसे बनाएं? एंड्रॉइड ऐप के लिए कस्टम पुश नोटिफिकेशन कैसे बनाएं?

    कस्टम लेआउट वाला पुश नोटिफिकेशन (RemoteViews + EditText) यह आर्टिकल कस्टम नोटिफिकेशन लेआउट में EditText डालकर यूजर इनपुट लेने का उदाहरण दिखाता है। ध्यान दें: नोटिफिकेशन में EditText इंटरेक्टिव नहीं होता (RemoteViews लिमिटेशन)। यह केवल डिस्प्ले के लिए है। चरण 1: प्रोजेक्ट और मेन लेआउट सामान्य बटन वा

  1. Android में रिमाइंडर नोटिफिकेशन कैसे बनाएं? Android में रिमाइंडर नोटिफिकेशन कैसे बनाएं?

    Android में रिमाइंडर नोटिफिकेशन बनाना यह उदाहरण दर्शाता है कि Android में रिमाइंडर नोटिफिकेशन कैसे बनाया जाता है। चरण 1 - नया प्रोजेक्ट बनाएं Android Studio में File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें। चरण 2 - res/layout/activity_main.xml में कोड जोड़ें <?