यह ट्यूटोरियल दर्शाता है कि नोटिफिकेशन पर टैप होने पर एक विशिष्ट Activity कैसे लॉन्च की जाए, PendingIntent और उचित इंटेंट फ्लैग का उपयोग करके सही नेविगेशन व्यवहार सुनिश्चित करते हुए।
मुख्य घटक
- PendingIntent.getActivity(): नोटिफिकेशन टैप पर एक्टिविटी लॉन्च करने के लिए रैप करता है
- इंटेंट फ्लैग:
FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOPडुप्लिकेट इंस्टेंस को रोकते हैं - NotificationCompat.Builder.setContentIntent(): नोटिफिकेशन से पेंडिंग इंटेंट जोड़ता है
चरण 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.notificationclick;
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_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 -> createNotificationWithClickAction());
}
private void createNotificationWithClickAction() {
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);
// Android 12+ (API 31) के लिए 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("इस एक्टिविटी को खोलने के लिए टैप करें")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.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_HIGH);
channel.setDescription("एक्टिविटी लॉन्च करने वाले नोटिफिकेशन");
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// नोटिफिकेशन से लॉन्च होने पर अतिरिक्त डेटा संभालें
if (intent.getBooleanExtra("from_notification", false)) {
// UI अपडेट करें, डेटा रीफ्रेश करें, आदि
}
}
}
इंटेंट फ्लैग समझाए गए
| फ्लैग | व्यवहार |
|---|---|
FLAG_ACTIVITY_CLEAR_TOP | यदि एक्टिविटी पहले से स्टैक में है, तो उसके ऊपर की सभी एक्टिविटी को साफ़ करता है और मौजूदा इंस्टेंस को डिलीवर करता है |
FLAG_ACTIVITY_SINGLE_TOP | यदि एक्टिविटी पहले से शीर्ष पर है, तो नया इंस्टेंस बनाने के बजाय onNewIntent() के माध्यम से नया इंटेंट डिलीवर करता है |
| संयुक्त | "सिंगल टॉप" व्यवहार सुनिश्चित करता है - डुप्लिकेट रोकता है, मौजूदा इंस्टेंस का पुन: उपयोग करता है |
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.notificationclick">
<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.NotificationClick">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"> <!-- XML में सिंगलटॉप सुदृढ़ करता है -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
सर्वोत्तम अभ्यास
- अलग लक्ष्य एक्टिविटी: गहरे लिंक के लिए
MainActivityके बजाय एक समर्पितNotificationTargetActivityका उपयोग करें - डेटा पास करें:
putExtra()के माध्यम से विशिष्ट पहचानकर्ता (उदाहरण के लिए, संदेश आईडी, चैट आईडी) शामिल करें - बैक स्टैक संभालें: जटिल नेविगेशन के लिए
TaskStackBuilderका उपयोग करें - PendingIntent फ्लैग अपडेट करें:
FLAG_UPDATE_CURRENTसुनिश्चित करता है कि नवीनतम अतिरिक्त डेटा उपयोग किया जाता है
उन्नत: TaskStackBuilder के साथ बैक स्टैक
// माता-पिता एक्टिविटी के साथ उचित बैक नेविगेशन के लिए
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(
0, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);