यह ट्यूटोरियल **NotificationCompat.Builder** और **PendingIntent** का उपयोग करके नोटिफिकेशन टैप पर एक्टिविटी लॉन्च करने का पूर्ण, आधुनिक कार्यान्वयन प्रदान करता है। इसमें उचित बैक स्टैक संचालन, अनुमतियाँ और एज केस शामिल हैं।
मुख्य घटक
- PendingIntent.getActivity(): नोटिफिकेशन टैप पर एक्टिविटी लॉन्च करता है
- इंटेंट फ्लैग:
CLEAR_TOP | SINGLE_TOPडुप्लिकेट रोकते हैं - setContentIntent(): नोटिफिकेशन से पेंडिंग इंटेंट जोड़ता है
- setAutoCancel(true): टैप पर नोटिफिकेशन खारिज करता है
चरण 1: प्रोजेक्ट सेटअप
एंड्रॉइड स्टूडियो → New Project → 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.notificationclickopen;
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 = "click_open_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 -> createNotification());
}
private void createNotification() {
createNotificationChannel();
// लक्ष्य एक्टिविटी के लिए इंटेंट
Intent notificationIntent = new Intent(this, MainActivity.class);
// महत्वपूर्ण फ्लैग: मौजूदा इंस्टेंस का पुन: उपयोग करें, नया न बनाएँ
notificationIntent.setFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
// पहचान के लिए डेटा पास करें
notificationIntent.putExtra("from_notification", true);
notificationIntent.putExtra("notification_timestamp", System.currentTimeMillis());
// Android 12+ के लिए FLAG_IMMUTABLE आवश्यक
PendingIntent pendingIntent = PendingIntent.getActivity(
this,
0,
notificationIntent,
PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
// नोटिफिकेशन बनाएँ
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("नोटिफिकेशन टैप डेमो")
.setContentText("मुख्य एक्टिविटी खोलने के लिए टैप करें")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("यह नोटिफिकेशन PendingIntent का उपयोग करके एक्टिविटी लॉन्च करने का प्रदर्शन करता है। टैप करने पर, यह नोटिफिकेशन खारिज हो जाएगा और ऐप खुल जाएगा।"))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.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_HIGH);
channel.setDescription("एक्टिविटी लॉन्च करने वाले नोटिफिकेशन");
channel.enableVibration(true);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// नए इंटेंट डेटा के साथ एक्टिविटी अपडेट करें
setIntent(intent);
// नोटिफिकेशन से लॉन्च होने पर संभालें
if (intent.getBooleanExtra("from_notification", false)) {
long timestamp = intent.getLongExtra("notification_timestamp", 0);
// UI अपडेट करें, डेटा रीफ्रेश करें, विशिष्ट स्क्रीन पर नेविगेट करें, आदि
}
}
}
चरण 4: AndroidManifest.xml - launchMode महत्वपूर्ण है
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.notificationclickopen">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.VIBRATE" />
<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.NotificationClickOpen">
<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>
इंटेंट फ्लैग विस्तार से
| फ्लैग | उद्देश्य |
|---|---|
FLAG_ACTIVITY_CLEAR_TOP | यदि लक्ष्य एक्टिविटी स्टैक में मौजूद है, तो उसके ऊपर की सभी एक्टिविटी नष्ट हो जाती हैं और लक्ष्य शीर्ष पर आ जाता है |
FLAG_ACTIVITY_SINGLE_TOP | यदि लक्ष्य पहले से शीर्ष पर है, तो नया इंस्टेंस बनाने के बजाय onNewIntent() कॉल करता है |
| संयुक्त प्रभाव | "मौजूदा इंस्टेंस ढूंढें, इसके ऊपर साफ़ करें, इसे शीर्ष पर लाएं, पुन: उपयोग करें" |
उन्नत: TaskStackBuilder के साथ उचित बैक नेविगेशन
यदि नोटिफिकेशन एक गहरी लिंक एक्टिविटी (मुख्य नहीं) खोलता है, तो उचित बैक स्टैक के लिए TaskStackBuilder का उपयोग करें:
// DetailActivity खोलना चाहते हैं जिसमें MainActivity पैरेंट के रूप में हो
Intent detailIntent = new Intent(this, DetailActivity.class);
detailIntent.putExtra("item_id", 123);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(detailIntent); // मैनिफेस्ट parentActivityName का उपयोग करता है
PendingIntent pendingIntent = stackBuilder.getPendingIntent(
0, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
AndroidManifest.xml में पैरेंट घोषणा आवश्यक
<activity
android:name=".DetailActivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
PendingIntent फ्लैग (Android 12+ API 31)
| फ्लैग | उपयोग करें जब |
|---|---|
FLAG_IMMUTABLE | हमेशा (API 31+ पर आवश्यक) |
FLAG_UPDATE_CURRENT | एक ही अनुरोध कोड के साथ नवीनतम अतिरिक्त डेटा चाहते हैं |
FLAG_CANCEL_CURRENT | नए से पहले मौजूदा PendingIntent कैंसिल करना चाहते हैं |
FLAG_NO_CREATE | केवल तभी प्राप्त करें यदि पहले से मौजूद हो, नया न बनाएँ |
सामान्य नुकसान और समाधान
| समस्या | कारण | समाधान |
|---|---|---|
| प्रत्येक टैप पर नई एक्टिविटी बनती है | launchMode="singleTop" अनुपस्थित | मैनिफेस्ट में android:launchMode="singleTop" जोड़ें |
| नोटिफिकेशन दिखाई नहीं देता | चैनल अनुपस्थित (API 26+) | createNotificationChannel() कॉल करें |
| Android 13+ पर क्रैश | POST_NOTIFICATIONS अनुमति अनुपस्थित | रनटाइम पर अनुमति अनुरोध करें |
| बासी डेटा दिखाता है | FLAG_UPDATE_CURRENT अनुपस्थित | PendingIntent में FLAG_UPDATE_CURRENT जोड़ें |
| बैक बटन ऐप से बाहर निकलता है | TaskStackBuilder उपयोग नहीं किया | गहरे लिंक के लिए TaskStackBuilder का उपयोग करें |
रनटाइम अनुमति अनुरोध (Android 13+)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS}, 1001);
}
}
}
परीक्षण चेकलिस्ट
- ऐप लॉन्च करें → नोटिफिकेशन बनाएँ → होम दबाएँ
- नोटिफिकेशन शेड खोलें → नोटिफिकेशन पर टैप करें
- सत्यापित करें: ऐप खुलता है, नोटिफिकेशन गायब होता है, कोई डुप्लिकेट एक्टिविटी नहीं
- बैक दबाएँ → होम स्क्रीन पर निकलता है (ऐप में नहीं फँसता)
- दोहराएँ → व्यवहार सुसंगत रहता है