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

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

यह ट्यूटोरियल दर्शाता है कि नोटिफिकेशन एक्शन बटन (जैसे "स्नूज़", "उत्तर दें", "खारिज करें") दबाए जाने पर नोटिफिकेशन शेड/पैनल को प्रोग्रामेटिक रूप से कैसे बंद किया जाए। मुख्य बात एक BroadcastReceiver का उपयोग करना है जो Intent.ACTION_CLOSE_SYSTEM_DIALOGS ब्रॉडकास्ट भेजता है।

दृष्टिकोण अवलोकन

  1. नोटिफिकेशन एक्शन बटन के लिए PendingIntent.getBroadcast() बनाएँ
  2. BroadcastReceiver में, सिस्टम डायलॉग बंद करने के लिए ब्रॉडकास्ट भेजें
  3. वांछित कार्रवाई करें (उदाहरण के लिए, स्नूज़ शेड्यूल करें, संदेश भेजें)

चरण 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: BroadcastReceiver - पैनल बंद करने की कुंजी

package com.example.notificationpanelclose;

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

public class NotificationActionReceiver extends BroadcastReceiver {

    public static final String ACTION_SNOOZE = "com.example.ACTION_SNOOZE";
    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) {
        String action = 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 (ACTION_SNOOZE.equals(action)) {
            // स्नूज़ लॉजिक: 10 मिनट बाद पुनः नोटिफिकेशन शेड्यूल करें
            scheduleSnoozeNotification(context, notificationId);
        } else if (ACTION_DISMISS.equals(action)) {
            // खारिज लॉजिक: नोटिफिकेशन कैंसिल करें
            cancelNotification(context, notificationId);
        }
    }

    private void scheduleSnoozeNotification(Context context, int originalId) {
        // WorkManager या AlarmManager का उपयोग करके पुनः शेड्यूल करें
        // सरलता के लिए तत्काल पुनः प्रदर्शन दिखा रहे हैं
        android.app.NotificationManager manager = 
            (android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(originalId);
        // यहां वास्तविक स्नूज़ शेड्यूलिंग लॉजिक जोड़ें
    }

    private void cancelNotification(Context context, int notificationId) {
        android.app.NotificationManager manager = 
            (android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(notificationId);
    }
}

चरण 4: MainActivity.java - एक्शन बटन के साथ नोटिफिकेशन बनाएँ

package com.example.notificationpanelclose;

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 = "action_button_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 -> createNotificationWithActions());
    }

    private void createNotificationWithActions() {
        createNotificationChannel();

        int notificationId = (int) System.currentTimeMillis();

        // स्नूज़ एक्शन के लिए PendingIntent (BroadcastReceiver का उपयोग करता है)
        Intent snoozeIntent = new Intent(this, NotificationActionReceiver.class);
        snoozeIntent.setAction(NotificationActionReceiver.ACTION_SNOOZE);
        snoozeIntent.putExtra(NotificationActionReceiver.EXTRA_NOTIFICATION_ID, notificationId);
        PendingIntent snoozePendingIntent = PendingIntent.getBroadcast(
            this, 0, snoozeIntent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);

        // खारिज एक्शन के लिए PendingIntent
        Intent dismissIntent = new Intent(this, NotificationActionReceiver.class);
        dismissIntent.setAction(NotificationActionReceiver.ACTION_DISMISS);
        dismissIntent.putExtra(NotificationActionReceiver.EXTRA_NOTIFICATION_ID, notificationId);
        PendingIntent dismissPendingIntent = PendingIntent.getBroadcast(
            this, 1, 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, 2, 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_launcher_foreground, "स्नूज़ (10 मिनट)", snoozePendingIntent)
            .addAction(R.drawable.ic_launcher_foreground, "खारिज करें", dismissPendingIntent)
            .setAutoCancel(true); // मुख्य सामग्री टैप पर ऑटो-कैंसिल

        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("एक्शन बटन वाले नोटिफिकेशन जो पैनल बंद करते हैं");
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);
        }
    }
}

चरण 5: AndroidManifest.xml में रिसीवर पंजीकृत करें

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

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

        <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>

        <!-- एक्शन बटन के लिए BroadcastReceiver -->
        <receiver
            android:name=".NotificationActionReceiver"
            android:exported="false">  <!-- आंतरिक उपयोग के लिए exported="false" -->
        </receiver>

    </application>
</manifest>

मुख्य बिंदु

  • Intent.ACTION_CLOSE_SYSTEM_DIALOGS: यह ब्रॉडकास्ट सिस्टम को नोटिफिकेशन शेड, हाल के ऐप्स आदि बंद करने का निर्देश देता है
  • BroadcastReceiver बनाम Activity: एक्शन बटन के लिए PendingIntent.getBroadcast() का उपयोग करें ताकि UI लॉन्च किए बिना पृष्ठभूमि में कार्रवाई संभाली जा सके
  • अद्वितीय अनुरोध कोड: प्रत्येक PendingIntent के लिए अलग-अलग अनुरोध कोड (0, 1, 2) टकराव रोकते हैं
  • exported="false": आंतरिक रिसीवर के लिए सुरक्षा सर्वोत्तम अभ्यास

नोट: Android संस्करण व्यवहार

Android संस्करणव्यवहार
Android 10 (API 29) और नीचेACTION_CLOSE_SYSTEM_DIALOGS विश्वसनीय रूप से काम करता है
Android 11+ (API 30+)गैर-सिस्टम ऐप्स के लिए प्रतिबंधित; अभी भी नोटिफिकेशन एक्शन से काम कर सकता है
Android 14+ (API 34)अधिक प्रतिबंध; परीक्षण आवश्यक

वैकल्पिक: RemoteInput के साथ इनलाइन उत्तर

Android 7.0+ (API 24) के लिए, पैनल खोले बिना सीधे उत्तर देने के लिए RemoteInput का उपयोग करने पर विचार करें:

RemoteInput remoteInput = new RemoteInput.Builder("reply_key")
    .setLabel("उत्तर दें")
    .build();

NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(
    R.drawable.ic_reply, "उत्तर दें", replyPendingIntent)
    .addRemoteInput(remoteInput)
    .build();

builder.addAction(replyAction);
  1. एंड्रॉइड में TextView का लेआउट वेट प्रोग्रामेटिकली कैसे सेट करें? एंड्रॉइड में TextView का लेआउट वेट प्रोग्रामेटिकली कैसे सेट करें?

    एंड्रॉइड में प्रोग्रामेटिकली Layout Weight सेट करना Layout Weight (android:layout_weight) केवल LinearLayout (या उसके सबक्लास) के चाइल्ड व्यूज पर काम करता है — RelativeLayout, ConstraintLayout, FrameLayout में इसका कोई प्रभाव नहीं पड़ता। मूल लेख का उदाहरण RelativeLayout में बटन का वेट सेट करने की कोश

  1. Android ऐप में होम बटन कैसे ओवरराइड करें? Android ऐप में होम बटन कैसे ओवरराइड करें?

    होम बटन दबाने का पता लगाने के लिए onUserLeaveHint() का उपयोग - लेकिन होम बटन को पूरी तरह ओवरराइड/ब्लॉक नहीं किया जा सकता। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: लेआउट res/layout/activity_main.xml: <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:

  1. एंड्रॉइड में बटन के कोने गोल कैसे करें एंड्रॉइड में बटन के कोने गोल कैसे करें

    ड्रॉएबल शेप का उपयोग करके बटन के कोने गोल बनाना एंड्रॉइड में बटन के कोने गोल करने के लिए XML ड्रॉएबल शेप का उपयोग करना सबसे सरल और प्रभावी तरीका है। यह विधि बटन के विभिन्न स्टेट्स (दबाया गया, फोकस्ड, सामान्य) के लिए अलग-अलग स्टाइल भी परिभाषित करने देती है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio