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

एंड्रॉइड ऐप के लिए कस्टम पुश नोटिफिकेशन कैसे बनाएं?

कस्टम लेआउट वाला पुश नोटिफिकेशन (RemoteViews + EditText)

यह आर्टिकल कस्टम नोटिफिकेशन लेआउट में EditText डालकर यूजर इनपुट लेने का उदाहरण दिखाता है। ध्यान दें: नोटिफिकेशन में EditText इंटरेक्टिव नहीं होता (RemoteViews लिमिटेशन)। यह केवल डिस्प्ले के लिए है।

चरण 1: प्रोजेक्ट और मेन लेआउट

सामान्य बटन वाला लेआउट।

चरण 2: कस्टम नोटिफिकेशन लेआउट — EditText के साथ

res/layout/custom_notification_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="96dp"
    android:padding="10dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_marginEnd="10dp"
        android:contentDescription="@string/app_name"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/image"
        android:text="Testing"
        android:textColor="#000"
        android:textSize="18sp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_marginTop="8dp"
        android:layout_toEndOf="@+id/image"
        android:hint="Enter something..."
        android:inputType="text"
        android:textSize="14sp" />
</RelativeLayout>

चरण 3: MainActivity

package app.tutorialspoint.com.notifyme;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RemoteViews;

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

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

    public void createNotification(View view) {
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this, default_notification_channel_id);

        mBuilder.setContent(contentView);
        mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
        mBuilder.setAutoCancel(true);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
            mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
            assert mNotificationManager != null;
            mNotificationManager.createNotificationChannel(notificationChannel);
        }

        assert mNotificationManager != null;
        mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
    }
}

चरण 4: मैनिफेस्ट

मानक।

⚠️ महत्वपूर्ण सीमाएं

  • RemoteViews में EditText इंटरेक्टिव नहीं होता — यूजर टाइप नहीं कर सकता
  • RemoteViews केवल इन व्यूज को सपोर्ट करता है: TextView, ImageView, Button, ImageButton, ProgressBar, Chronometer, AnalogClock, ViewFlipper, ListView, GridView, StackView, AdapterViewFlipper
  • EditText, WebView, कस्टम व्यू — सपोर्टेड नहीं

असली इंटरैक्टिव नोटिफिकेशन के लिए

  1. एक्शन बटन — addAction() से बटन जोड़ें, हर बटन का अपना PendingIntent
  2. Reply Action (Direct Reply) — Android 7.0+ में RemoteInput से इंलाइन रिप्लाई:
    RemoteInput remoteInput = new RemoteInput.Builder("key_text_reply")
                .setLabel("Reply")
                .build();
    
    NotificationCompat.Action action = new NotificationCompat.Action.Builder(
        R.drawable.ic_reply, "Reply", pendingIntent)
        .addRemoteInput(remoteInput)
        .build();
  3. कस्टम एक्टिविटी लॉन्च — नोटिफिकेशन टैप पर फुल एक्टिविटी खोलें जहां EditText काम करे

पुश नोटिफिकेशन (FCM) के लिए

सर्वर से डेटा पेलोड भेजें, क्लाइंट पर FirebaseMessagingService.onMessageReceived() में पार्स करके नोटिफिकेशन बनाएं।

  1. एंड्रॉइड ऐप में TextToSpeech कैसे बनाएं? एंड्रॉइड ऐप में TextToSpeech कैसे बनाएं?

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

  1. Facebook पर Android ऐप कैसे बनाएं? Facebook पर Android ऐप कैसे बनाएं?

    Facebook Developer Console पर ऐप बनाकर App ID प्राप्त करने की चरण-दर-चरण प्रक्रिया। चरण 1: Facebook Developer Console पर ऐप बनाएं developers.facebook.com पर जाएं My Apps → Create App चुनें ऐप नाम और ईमेल दर्ज करें Create App ID पर क्लिक करें चरण 2: Android Studio में SDK जोड़ें प्रोजेक्ट-लेवल bu

  1. Android में कस्टम नोटिफिकेशन लेआउट और टेक्स्ट कलर कैसे बनाएं? Android में कस्टम नोटिफिकेशन लेआउट और टेक्स्ट कलर कैसे बनाएं?

    Android में कस्टम नोटिफिकेशन लेआउट बनाने का पूरा गाइड यह उदाहरण RemoteViews और NotificationCompat.Builder का उपयोग करके पूरी तरह से कस्टमाइज्ड नोटिफिकेशन (कस्टम लेआउट, इमेज, टेक्स्ट कलर, बटन) बनाने का तरीका दिखाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: म