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

एंड्रॉइड में नोटिफिकेशन क्लिक पर एक्टिविटी कैसे शुरू करें?

यह ट्यूटोरियल **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);
        }
    }
}

परीक्षण चेकलिस्ट

  1. ऐप लॉन्च करें → नोटिफिकेशन बनाएँ → होम दबाएँ
  2. नोटिफिकेशन शेड खोलें → नोटिफिकेशन पर टैप करें
  3. सत्यापित करें: ऐप खुलता है, नोटिफिकेशन गायब होता है, कोई डुप्लिकेट एक्टिविटी नहीं
  4. बैक दबाएँ → होम स्क्रीन पर निकलता है (ऐप में नहीं फँसता)
  5. दोहराएँ → व्यवहार सुसंगत रहता है
  1. Android में Notification.deleteIntent का उपयोग कैसे करें? Android में Notification.deleteIntent का उपयोग कैसे करें?

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

  1. Android में नोटिफिकेशन से सर्विस कैसे शुरू करें? Android में नोटिफिकेशन से सर्विस कैसे शुरू करें?

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

  1. Android में बटन क्लिक पर नई एक्टिविटी कैसे शुरू करें Android में बटन क्लिक पर नई एक्टिविटी कैसे शुरू करें

    परिचयएंड्रॉइड में एक एक्टिविटी से दूसरी एक्टिविटी पर जाने के लिए `Intent` का उपयोग किया जाता है। यह लेख बटन क्लिक पर नई एक्टिविटी शुरू करने का बुनियादी उदाहरण दिखाता है।कार्यान्वयनचरण 1: लेआउट (activity_main.xml)<Button android:id=@+id/button android:layout_width=wrap_content andro