नोटिफिकेशन में वाइब्रेशन + साउंड दोनों
नोटिफिकेशन बिल्डर पर setVibrate() और setSound() का उपयोग करके दोनों प्रभाव जोड़ें। Android 8.0+ के लिए चैनल में भी सेट करें।
चरण 1-2: प्रोजेक्ट और लेआउट
चरण 3: MainActivity.java
package app.tutorialspoint.com.notifyme;
import android.app.NotificationManager;
import android.content.Context;
import android.media.RingtoneManager;
import android.net.Uri;
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 {
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 -> {
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}) // 1 सेकंड ऑन, 1 ऑफ, 5 बार
.setContentTitle("वाइब्रेशन + साउंड")
.setSound(alarmSound)
.setContentText("दोनों प्रभावों के साथ नोटिफिकेशन")
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify((int) System.currentTimeMillis(), builder.build());
});
}
}
चरण 4: AndroidManifest.xml - VIBRATE परमिशन जरूरी
<uses-permission android:name="android.permission.VIBRATE" />
वाइब्रेशन पैटर्न समझें
long[] pattern = {0, 500, 200, 500, 200, 500};
// 0ms देरी, 500ms वाइब्रेट, 200ms रुकें, 500ms वाइब्रेट...
// पैटर्न दोहराने के लिए: .setVibrate(pattern) और चैनल में setVibrationPattern()
Android 8.0+ के लिए चैनल कॉन्फिगरेशन
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "नाम", IMPORTANCE_HIGH);
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{0, 500, 200, 500});
channel.setSound(alarmSound, audioAttributes);
manager.createNotificationChannel(channel);
}
महत्वपूर्ण नोट्स
- वाइब्रेशन के लिए
VIBRATEपरमिशन अनिवार्य है। - यूजर सेटिंग्स से वाइब्रेशन/साउंड डिसेबल कर सकता है - ऐप इसे ओवरराइड नहीं कर सकता।
- डू नॉट डिस्टर्ब मोड में वाइब्रेशन/साउंड व्यवहार चैनल के महत्व पर निर्भर करता है।