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

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

नोटिफिकेशन टैप पर एक्टिविटी लॉन्च करना

PendingIntent के साथ NotificationCompat.Builder का उपयोग करके नोटिफिकेशन पर क्लिक करने पर किसी एक्टिविटी को खोल सकते हैं।

चरण 1: नया प्रोजेक्ट बनाएं

चरण 2: लेआउट (res/layout/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 app.tutorialspoint.com.notifyme;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    public static final String NOTIFICATION_CHANNEL_ID = "10001";
    private static final String DEFAULT_CHANNEL_ID = "default";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnCreateNotification = findViewById(R.id.btnCreateNotification);
        btnCreateNotification.setOnClickListener(v -> {
            // टैप पर खुलने वाली एक्टिविटी
            Intent intent = new Intent(MainActivity.this, MainActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(
                MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
            );

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentIntent(contentIntent)
                .setContentTitle("टेस्ट")
                .setContentText("नमस्ते! यह मेरा पहला पुश नोटिफिकेशन है")
                .setAutoCancel(true); // टैप पर हट जाए

            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            // Android 8.0+ के लिए चैनल जरूरी
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                int importance = NotificationManager.IMPORTANCE_HIGH;
                NotificationChannel channel = new NotificationChannel(
                    NOTIFICATION_CHANNEL_ID, "चैनल नाम", importance);
                builder.setChannelId(NOTIFICATION_CHANNEL_ID);
                assert manager != null;
                manager.createNotificationChannel(channel);
            }

            assert manager != null;
            manager.notify((int) System.currentTimeMillis(), builder.build());
        });
    }
}

चरण 4: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.tutorialspoint.com.notifyme">

    <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/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

महत्वपूर्ण नोट्स

  • FLAG_IMMUTABLE - Android 12+ (API 31+) के लिए अनिवार्य है जब PendingIntent किसी अन्य ऐप को नहीं भेजा जा रहा।
  • Notification Channel - Android 8.0 (API 26) से अनिवार्य है।
  • setAutoCancel(true) - यूजर द्वारा टैप करने पर नोटिफिकेशन ऑटोमैटिकली हट जाता है।
  • अलग एक्टिविटी खोलने के लिए Intent में उस एक्टिविटी की क्लास डालें।
  1. एंड्रॉइड एक्टिविटी से रिंगटोन कैसे सेट करें? एंड्रॉइड एक्टिविटी से रिंगटोन कैसे सेट करें?

    एंड्रॉइड में RingtoneManager से रिंगटोन चुनना और सेट करना एंड्रॉइड में सिस्टम रिंगटोन पिकर लॉन्च करने के लिए RingtoneManager.ACTION_RINGTONE_PICKER इंटेंट उपयोग करें। यूज़र द्वारा चुनी गई रिंगटोन का URI onActivityResult में मिलता है। **नोट:** Android 10+ (API 29) में WRITE_SETTINGS परमिशन से सिस्टम

  1. एंड्रॉइड में एक्टिविटी से फ्रैगमेंट में वेरिएबल कैसे पास करें? एंड्रॉइड में एक्टिविटी से फ्रैगमेंट में वेरिएबल कैसे पास करें?

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

  1. एंड्रॉइड में फ्रैगमेंट से एक्टिविटी का मेथड कैसे कॉल करें एंड्रॉइड में फ्रैगमेंट से एक्टिविटी का मेथड कैसे कॉल करें

    फ्रैगमेंट से होस्ट एक्टिविटी के पब्लिक मेथड को कॉल करने के तरीके एंड्रॉइड में फ्रैगमेंट अपनी होस्ट एक्टिविटी के साथ getActivity() के माध्यम से संवाद कर सकता है। एक्टिविटी के पब्लिक मेथड को कॉल करने के लिए फ्रैगमेंट को एक्टिविटी को उपयुक्त टाइप में कास्ट करना होता है। यह पैटर्न फ्रैगमेंट-एक्टिविटी सं