यह ट्यूटोरियल दर्शाता है कि एंड्रॉइड नोटिफिकेशन को प्रोग्रामेटिक रूप से कुछ सेकंड बाद स्वचालित रूप से कैसे हटाया जाए। हम एक Handler का उपयोग करके विलंबित निष्पादन शेड्यूल करेंगे जो NotificationManager.cancel() को कॉल करता है।
पूर्वापेक्षाएँ
- एंड्रॉइड स्टूडियो स्थापित
- बुनियादी एंड्रॉइड विकास ज्ञान
- API लेवल 26+ (नोटिफिकेशन चैनल के लिए)
चरण 1: नया प्रोजेक्ट बनाएँ
एंड्रॉइड स्टूडियो खोलें → File → New Project → "Empty Activity" चुनें → आवश्यक विवरण भरें → Finish पर क्लिक करें।
चरण 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.autoclearnotification;
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 android.os.Handler;
import android.os.Looper;
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_clear_channel";
private static final String CHANNEL_NAME = "ऑटो-क्लियर नोटिफिकेशन";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnCreateNotification = findViewById(R.id.btnCreateNotification);
btnCreateNotification.setOnClickListener(v -> createAutoClearNotification());
}
private void createAutoClearNotification() {
// नोटिफिकेशन चैनल बनाएँ (Android 8.0+ के लिए आवश्यक)
createNotificationChannel();
// मुख्य गतिविधि खोलने के लिए इंटेंट
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
// नोटिफिकेशन बनाएँ
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("ऑटो-क्लियर नोटिफिकेशन")
.setContentText("यह नोटिफिकेशन 5 सेकंड में गायब हो जाएगा")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setAutoCancel(false); // मैन्युअल क्लियर रोकें
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int notificationId = (int) System.currentTimeMillis();
notificationManager.notify(notificationId, builder.build());
// 5 सेकंड (5000ms) बाद नोटिफिकेशन कैंसिल करने के लिए हैंडलर शेड्यूल करें
new Handler(Looper.getMainLooper()).postDelayed(() -> {
notificationManager.cancel(notificationId);
}, 5000);
}
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);
}
}
}
चरण 4: AndroidManifest.xml अपडेट करें
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.autoclearnotification">
<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.AutoClearNotification">
<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>
मुख्य बिंदु
- Handler + postDelayed(): UI थ्रेड पर विलंबित निष्पादन के लिए प्रयुक्त
- notificationManager.cancel(id): विशिष्ट नोटिफिकेशन हटाता है
- अद्वितीय आईडी:
System.currentTimeMillis()प्रत्येक नोटिफिकेशन के लिए अद्वितीय आईडी सुनिश्चित करता है - नोटिफिकेशन चैनल: Android 8.0 (API 26) और इसके बाद के संस्करण के लिए अनिवार्य
आधुनिक विकल्प (WorkManager / AlarmManager)
ऐप प्रोसेस समाप्त होने के बाद भी विश्वसनीय विलंबित निष्पादन के लिए, WorkManager (सटीक समय नहीं) या AlarmManager.setExact() के साथ BroadcastReceiver का उपयोग करने पर विचार करें।