नोटिफिकेशन में वाइब्रेशन और LED लाइट जोड़ना
नोटिफिकेशन चैनल (API 26+) में setVibrationPattern(), enableVibration(), setLightColor() से वाइब्रेशन और नोटिफिकेशन लाइट कॉन्फिगर करें।
चरण 1: प्रोजेक्ट और मेन लेआउट
सामान्य बटन वाला लेआउट।
चरण 2: कस्टम नोटिफिकेशन लेआउट
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);
mBuilder.setVisibility(NotificationCompat.VISIBILITY_SECRET);
// वाइब्रेशन पैटर्न: {देरी, वाइब्रेट, देरी, वाइब्रेट...} मिलीसेकंड में
long[] VIBRATE_PATTERN = {0, 500, 200, 300};
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);
// LED लाइट रंग सेट करें (डिवाइस सपोर्ट करे तो)
notificationChannel.setLightColor(getResources().getColor(R.color.colorAccent));
// वाइब्रेशन पैटर्न और इनेबल
notificationChannel.setVibrationPattern(VIBRATE_PATTERN);
notificationChannel.enableVibration(true);
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
assert mNotificationManager != null;
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
}
}
चरण 4: मैनिफेस्ट — वाइब्रेट परमिशन जरूरी
<uses-permission android:name="android.permission.VIBRATE" />
वाइब्रेशन पैटर्न समझें
long[] pattern = {delay, vibrate, delay, vibrate, ...}
{0, 500}— तुरंत 500ms वाइब्रेट{0, 500, 200, 300}— 500ms वाइब्रेट, 200ms रुकें, 300ms वाइब्रेट{0, 200, 100, 200, 100, 200}— तीन छोटे वाइब्रेशन
LED लाइट के बारे में
- सभी डिवाइस में नोटिफिकेशन LED नहीं होता
setLightColor()केवल LED वाले डिवाइस पर काम करता है- रंग:
Color.RED,Color.GREEN,Color.BLUE, या कस्टम ARGB - ब्लिंक रेट सिस्टम कंट्रोल करता है
नोटिफिकेशन बिल्डर पर सीधे सेट करना (पुराना API)
API 26 से पहले:
mBuilder.setVibrate(new long[]{0, 500, 200, 300});
mBuilder.setLights(Color.RED, 500, 500); // रंग, ऑन-टाइम, ऑफ-टाइम
लेकिन API 26+ में चैनल पर सेट करना अनिवार्य है — बिल्डर पर सेट करने का कोई असर नहीं होता।