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

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

यह ट्यूटोरियल दर्शाता है कि नोटिफिकेशन पर टैप होने पर एक विशिष्ट 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>

सर्वोत्तम अभ्यास

  1. अलग लक्ष्य एक्टिविटी: गहरे लिंक के लिए MainActivity के बजाय एक समर्पित NotificationTargetActivity का उपयोग करें
  2. डेटा पास करें: putExtra() के माध्यम से विशिष्ट पहचानकर्ता (उदाहरण के लिए, संदेश आईडी, चैट आईडी) शामिल करें
  3. बैक स्टैक संभालें: जटिल नेविगेशन के लिए TaskStackBuilder का उपयोग करें
  4. 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);
  1. एंड्रॉइड में एक्टिविटी में होम की प्रेस कैसे डिटेक्ट करें? एंड्रॉइड में एक्टिविटी में होम की प्रेस कैसे डिटेक्ट करें?

    एंड्रॉइड में होम बटन प्रेस डिटेक्शन यह गाइड onUserLeaveHint() लाइफसाइकिल मेथड का उपयोग करके होम बटन प्रेस डिटेक्ट करने का तरीका बताता है, और इसकी सीमाओं को स्पष्ट करता है। महत्वपूर्ण: सीमाएं ⚠️ नोट: एंड्रॉइड सुरक्षा कारणों से होम बटन को सीधे इंटरसेप्ट या ब्लॉक करने की अनुमति नहीं देता। on

  1. एंड्रॉइड में एक्टिविटी को कैसे रीस्टार्ट करें? एंड्रॉइड में एक्टिविटी को कैसे रीस्टार्ट करें?

    एंड्रॉइड एक्टिविटी को प्रोग्रामेटिकली रीस्टार्ट करना यह गाइड एक्टिविटी को रीस्टार्ट करने के विभिन्न तरीकों को दर्शाता है, जिसमें इंटेंट फ्लैग्स और रिक्रिएशन शामिल हैं। लेआउट (activity_main.xml) <RelativeLayout xmlns:android=https://schemas.android.com/apk/res/android android:layout_width=mat

  1. Android में उपयोगकर्ता चयन पर ऐप भाषा कैसे बदलें? Android में उपयोगकर्ता चयन पर ऐप भाषा कैसे बदलें?

    Android में रनटाइम भाषा परिवर्तन (Locale बदलना) यह उदाहरण दर्शाता है कि Spinnner से भाषा चुनकर ऐप की भाषा को रनटाइम पर कैसे बदला जाए, बिना ऐप रीस्टार्ट किए (एक्टिविटी रीक्रिएट करके)। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: स्ट्रिंग रिसोर्स फ़ाइलें बनाएं डिफ़