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

एंड्रॉइड में उपयोगकर्ता के क्लिक करने पर नोटिफिकेशन कैसे बंद करें?

यह गाइड बताता है कि नोटिफिकेशन को कैसे कॉन्फ़िगर किया जाए ताकि यह उपयोगकर्ता द्वारा टैप करने पर **स्वचालित रूप से खारिज** हो जाए - यह अधिकांश नोटिफिकेशन के लिए डिफ़ॉल्ट और अनुशंसित व्यवहार है।

मुख्य सेटिंग: setAutoCancel(true)

NotificationCompat.Builder.setAutoCancel(true) विधि नोटिफिकेशन को उपयोगकर्ता द्वारा क्लिक करने पर स्वचालित रूप से हटने का निर्देश देती है। यह डिफ़ॉल्ट व्यवहार है, लेकिन इसे स्पष्ट रूप से सेट करना कोड को स्पष्ट बनाता है।

चरण 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.autocancelnotification;

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 = "auto_cancel_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 -> showAutoCancelNotification());
    }

    private void showAutoCancelNotification() {
        createNotificationChannel();

        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("opened_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 notificationId = (int) System.currentTimeMillis();
        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_DEFAULT);
            channel.setDescription("क्लिक करने पर स्वतः खारिज होने वाले नोटिफिकेशन");
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent.getBooleanExtra("opened_from_notification", false)) {
            // नोटिफिकेशन से खोले जाने पर कार्रवाई करें
        }
    }
}

व्यवहार सारांश

क्रियापरिणाम
उपयोगकर्ता नोटिफिकेशन पर टैप करता हैनोटिफिकेशन खारिज होता है → PendingIntent लॉन्च होता है → एक्टिविटी खुलती है
उपयोगकर्ता नोटिफिकेशन को स्वाइप करता हैनोटिफिकेशन खारिज होता है (कोई कार्रवाई नहीं)
setAutoCancel(false)नोटिफिकेशन टैप/स्वाइप दोनों पर बना रहता है

AndroidManifest.xml

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

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

        <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>
    </application>
</manifest>

सामान्य गलतियों से बचें

  • PendingIntent फ्लैग भूलना: Android 12+ पर हमेशा FLAG_IMMUTABLE (या FLAG_MUTABLE यदि डेटा बदलना हो) का उपयोग करें
  • गलत लॉन्च मोड: मैनिफेस्ट में android:launchMode="singleTop" के बिना, प्रत्येक टैप एक नई एक्टिविटी इंस्टेंस बनाता है
  • नोटिफिकेशन चैनल नहीं: Android 8.0+ पर चैनल के बिना नोटिफिकेशन दिखाई नहीं देंगे

उन्नत: कस्टम खारिज लॉजिक

यदि आपको खारिज होने पर कस्टम लॉजिक की आवश्यकता है (उदाहरण के लिए, एनालिटिक्स लॉग करें, सर्वर सिंक करें), तो setAutoCancel(true) के बजाय BroadcastReceiver के साथ PendingIntent.getBroadcast() का उपयोग करें:

// BroadcastReceiver दृष्टिकोण
Intent dismissIntent = new Intent(this, NotificationDismissReceiver.class);
dismissIntent.putExtra("notification_id", notificationId);
PendingIntent dismissPendingIntent = PendingIntent.getBroadcast(
    this, 0, dismissIntent, PendingIntent.FLAG_IMMUTABLE);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
    // ...
    .setDeleteIntent(dismissPendingIntent)  // स्वाइप/खारिज होने पर ट्रिगर होता है
    .setContentIntent(contentPendingIntent); // टैप होने पर ट्रिगर होता है
  1. Android में रिमाइंडर नोटिफिकेशन कैसे बनाएं? Android में रिमाइंडर नोटिफिकेशन कैसे बनाएं?

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

  1. Android में EditText के बाहर क्लिक करने पर सॉफ्ट कीबोर्ड कैसे छुपाएं? Android में EditText के बाहर क्लिक करने पर सॉफ्ट कीबोर्ड कैसे छुपाएं?

    Android में EditText के बाहर टच पर सॉफ्ट कीबोर्ड छुपाना यह उदाहरण दर्शाता है कि जब उपयोगकर्ता EditText के बाहर कहीं टैप करता है, तो सॉफ्ट कीबोर्ड को प्रोग्रामेटिकली कैसे छुपाया जाए। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: लेआउट फ़ाइल (res/layout/activity_main

  1. विंडोज फीडबैक अधिसूचना को हमेशा के लिए कैसे बंद करें विंडोज फीडबैक अधिसूचना को हमेशा के लिए कैसे बंद करें

    माइक्रोसॉफ्ट पॉपअप के माध्यम से हर अब और फिर आपके विंडोज अनुभव पर प्रतिक्रिया मांगता है। यदि आप उन पॉपअप से छुटकारा चाहते हैं, तो एक बार के ट्वीक के साथ ऐसा करना आसान है। सेटिंग्स > गोपनीयता > फीडबैक और डायग्नोस्टिक्स पर जाएं और फीडबैक आवृत्ति के तहत ड्रॉपडाउन में, नेवर चुनें। बस! आप फिर से उ