यह गाइड **कस्टम नोटिफिकेशन लेआउट (RemoteViews)** का उपयोग करके नोटिफिकेशन पृष्ठभूमि रंग को अनुकूलित करने का तरीका दिखाता है। मानक नोटिफिकेशन सिस्टम थीम रंगों का उपयोग करते हैं, लेकिन RemoteViews पूर्ण दृश्य नियंत्रण की अनुमति देता है।
मुख्य अवधारणा: RemoteViews + setCustomContentView()
मानक NotificationCompat.Builder विधियाँ पृष्ठभूमि रंग नहीं बदल सकतीं। इसके बजाय, एक कस्टम लेआउट XML बनाएँ और इसे RemoteViews के माध्यम से लागू करें।
चरण 1: कस्टम नोटिफिकेशन लेआउट (res/layout/custom_notification.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/notification_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/notification_bg_color" <!-- कस्टम पृष्ठभूमि रंग -->
android:padding="16dp">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginEnd="16dp"
android:contentDescription="@string/app_name"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/iv_icon"
android:layout_alignTop="@id/iv_icon"
android:text="कस्टम शीर्षक"
android:textColor="@color/notification_text_primary"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/iv_icon"
android:layout_below="@id/tv_title"
android:layout_marginTop="4dp"
android:text="कस्टम पृष्ठभूमि रंग के साथ नोटिफिकेशन संदेश"
android:textColor="@color/notification_text_secondary"
android:textSize="14sp" />
<!-- प्रगति बार (वैकल्पिक) -->
<ProgressBar
android:id="@+id/pb_progress"
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_below="@id/tv_message"
android:layout_marginTop="8dp"
android:indeterminate="false"
android:max="100"
android:progress="0"
android:visibility="gone" />
</RelativeLayout>
चरण 2: रंग परिभाषित करें (res/values/colors.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="notification_bg_color">#E3F2FD</color> <!-- हल्का नीला -->
<color name="notification_text_primary">#1565C0</color> <!-- गहरा नीला -->
<color name="notification_text_secondary">#1E88E5</color> <!-- मध्यम नीला -->
<color name="notification_bg_success">#E8F5E9</color> <!-- हल्का हरा -->
<color name="notification_bg_warning">#FFF3E0</color> <!-- हल्का नारंगी -->
<color name="notification_bg_error">#FBE9E7</color> <!-- हल्का लाल -->
</resources>
चरण 3: MainActivity.java - RemoteViews लागू करें
package com.example.custombgnotification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;
public class MainActivity extends AppCompatActivity {
private static final String CHANNEL_ID = "custom_bg_channel";
private static final String CHANNEL_NAME = "कस्टम BG नोटिफिकेशन";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNotificationChannel();
Button btnDefault = findViewById(R.id.btnDefaultBg);
Button btnSuccess = findViewById(R.id.btnSuccessBg);
Button btnWarning = findViewById(R.id.btnWarningBg);
Button btnError = findViewById(R.id.btnErrorBg);
Button btnDynamic = findViewById(R.id.btnDynamicBg);
btnDefault.setOnClickListener(v -> showCustomNotification(R.color.notification_bg_color));
btnSuccess.setOnClickListener(v -> showCustomNotification(R.color.notification_bg_success));
btnWarning.setOnClickListener(v -> showCustomNotification(R.color.notification_bg_warning));
btnError.setOnClickListener(v -> showCustomNotification(R.color.notification_bg_error));
btnDynamic.setOnClickListener(v -> showDynamicColorNotification());
}
private void showCustomNotification(int bgColorRes) {
// RemoteViews के साथ कस्टम लेआउट बनाएँ
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
// पृष्ठभूमि रंग सेट करें (RemoteViews setInt का उपयोग करता है)
contentView.setInt(R.id.notification_root, "setBackgroundColor",
getColor(bgColorRes));
// टेक्स्ट सेट करें
contentView.setTextViewText(R.id.tv_title, "कस्टम पृष्ठभूमि नोटिफिकेशन");
contentView.setTextViewText(R.id.tv_message, "पृष्ठभूमि रंग: " + getResources().getResourceEntryName(bgColorRes));
// मुख्य सामग्री इंटेंट
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
// नोटिफिकेशन बनाएँ
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setCustomContentView(contentView) // ← कस्टम लेआउट यहाँ सेट करें
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify((int) System.currentTimeMillis(), builder.build());
}
private void showDynamicColorNotification() {
// रनटाइम पर डायनामिक रंग (उदाहरण के लिए, प्रगति/स्थिति के आधार पर)
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
// डायनामिक रंग: हरा (सफलता)
int dynamicColor = Color.parseColor("#4CAF50"); // या किसी भी रनटाइम गणना से
contentView.setInt(R.id.notification_root, "setBackgroundColor", dynamicColor);
contentView.setTextViewText(R.id.tv_title, "डायनामिक रंग नोटिफिकेशन");
contentView.setTextViewText(R.id.tv_message, "रनटाइम पर गणना किया गया रंग: #4CAF50");
contentView.setTextColor(R.id.tv_title, Color.WHITE);
contentView.setTextColor(R.id.tv_message, Color.WHITE);
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setCustomContentView(contentView)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify((int) System.currentTimeMillis(), builder.build());
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= 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);
}
}
}
चरण 4: लेआउट (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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="नोटिफिकेशन पृष्ठभूमि रंग चुनें:"
android:textSize="16sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/btnDefaultBg"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="डिफ़ॉल्ट नीला"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/btnSuccessBg"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="सफलता हरा"
android:layout_marginTop="12dp"
app:layout_constraintTop_toBottomOf="@id/btnDefaultBg"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/btnWarningBg"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="चेतावनी नारंगी"
android:layout_marginTop="12dp"
app:layout_constraintTop_toBottomOf="@id/btnSuccessBg"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/btnErrorBg"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="त्रुटि लाल"
android:layout_marginTop="12dp"
app:layout_constraintTop_toBottomOf("@id/btnWarningBg")
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/btnDynamicBg"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="डायनामिक रंग (रनटाइम)"
android:layout_marginTop="12dp"
app:layout_constraintTop_toBottomOf="@id/btnErrorBg"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
चरण 5: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.custombgnotification">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<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.CustomBgNotification">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
RemoteViews विधियाँ संदर्भ
| विधि | उपयोग |
|---|---|
setInt(viewId, "setBackgroundColor", color) | किसी भी व्यू पर पृष्ठभूमि रंग सेट करें |
setTextViewText(viewId, text) | TextView टेक्स्ट सेट करें |
setTextColor(viewId, color) | TextView टेक्स्ट रंग सेट करें |
setImageViewResource(viewId, resId) | ImageView छवि सेट करें |
setViewVisibility(viewId, View.VISIBLE/GONE) | दृश्यता टॉगल करें |
setProgressBar(viewId, max, progress, indeterminate) | ProgressBar अपडेट करें |
सीमाएँ और विचार
| पहलू | विवरण |
|---|---|
| लेआउट आकार | सीमित ऊँचाई (~256dp विस्तारित, ~64dp संकुचित) |
| समर्थित व्यू | केवल मूल व्यू: TextView, ImageView, ProgressBar, Chronometer, Button, AnalogClock, LinearLayout, RelativeLayout, FrameLayout, GridLayout |
| कोई WebView/RecyclerView | जटिल व्यू समर्थित नहीं |
| कोई कस्टम फॉन्ट | केवल सिस्टम फॉन्ट; टाइपफेस के लिए setTypeface उपयोग करें (सीमित) |
| डार्क मोड | रंगों के लिए क्वालिफायर के साथ नाइट वेरिएंट बनाएँ |
उन्नत: विस्तारित लेआउट (BigContentView)
// संकुचित दृश्य (लॉक स्क्रीन, शेड में)
RemoteViews collapsedView = new RemoteViews(getPackageName(), R.layout.notification_collapsed);
// विस्तारित दृश्य (उपयोगकर्ता नीचे खींचता है)
RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.notification_expanded);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setCustomContentView(collapsedView) // संकुचित
.setCustomBigContentView(expandedView) // विस्तारित
.setStyle(new NotificationCompat.DecoratedCustomViewStyle()); // सिस्टम डेकोरेशन जोड़ता है
डार्क मोड समर्थन
<!-- res/values-night/colors.xml -->
<resources>
<color name="notification_bg_color">#1A237E</color> <!-- डार्क मोड: गहरा नीला -->
<color name="notification_text_primary">#BBDEFB</color> <!-- डार्क मोड: हल्का नीला -->
<color name="notification_text_secondary">#90CAF9</color> <!-- डार्क मोड: मध्यम नीला -->
</resources>
सर्वोत्तम अभ्यास
- सिस्टम रंगों का सम्मान करें: जहां संभव हो
?android:attr/colorBackgroundजैसे थीम विशेषताओं का उपयोग करें - परीक्षण करें: लॉक स्क्रीन, नोटिफिकेशन शेड, विस्तारित/संकुचित अवस्थाओं पर
- सरल रखें: जटिल लेआउट कुछ उपकरणों पर टूट सकते हैं
- स्मॉल आइकन आवश्यक: कस्टम लेआउट के साथ भी
setSmallIcon()अनिवार्य है - दक्षता: RemoteViews सीरियलाइज़ेशन ओवरहेड जोड़ता है; विवेकपूर्ण उपयोग करें