नोटिफिकेशन के लिए कस्टम रिंगटोन चुनना और बजाना
यूजर को रिंगटोन पिकर से साउंड चुनने दें, फिर नोटिफिकेशन में उस URI को सेट करें। RingtoneManager.ACTION_RINGTONE_PICKER इंटेंट उपयोग करें।
चरण 1: प्रोजेक्ट और लेआउट — दो बटन
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:onClick="setRingtone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="Set Ringtone" />
<Button
android:onClick="createNotification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="16dp"
android:text="Create Notification" />
</RelativeLayout>
चरण 2: MainActivity — रिंगटोन पिकर + नोटिफिकेशन
package app.tutorialspoint.com.notifyme;
import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
public static final String NOTIFICATION_CHANNEL_ID = "10001";
private final static String default_notification_channel_id = "default";
String chosenRingtone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onNewIntent(getIntent());
}
// रिंगटोन पिकर खोलें
public void setRingtone(View view) {
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
this.startActivityForResult(intent, 5);
}
// नोटिफिकेशन बनाएं — चुनी हुई रिंगटोन के साथ
public void createNotification(View view) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this, default_notification_channel_id);
mBuilder.setContentTitle("Notify Me");
mBuilder.setContentText("Something important!");
mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
mBuilder.setAutoCancel(true);
// कस्टम साउंड सेट करें (अगर चुनी गई है)
if (chosenRingtone != null) {
mBuilder.setSound(Uri.parse(chosenRingtone));
}
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);
// चैनल पर भी डिफॉल्ट साउंड सेट कर सकते हैं
if (chosenRingtone != null) {
notificationChannel.setSound(Uri.parse(chosenRingtone), null);
}
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
assert mNotificationManager != null;
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == 5) {
Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (uri != null) {
this.chosenRingtone = uri.toString();
} else {
this.chosenRingtone = null;
}
}
}
}
चरण 3: मैनिफेस्ट
मानक।
महत्वपूर्ण बिंदु
- बिल्डर पर setSound() — API 26 से पहले काम करता है
- चैनल पर setSound() — API 26+ के लिए अनिवार्य (बिल्डर पर सेट करने का असर नहीं)
- चैनल बनने के बाद उसका साउंड बदल नहीं सकते — नया चैनल बनाना पड़ता है
- रिंगटोन URI को SharedPreferences में सेव करें ताकि ऐप रीस्टार्ट पर याद रहे
रॉ फाइल से साउंड (ऐप में बंडल)
// res/raw/notification_sound.mp3 रखें
Uri soundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification_sound);
mBuilder.setSound(soundUri);
साइलेंट नोटिफिकेशन
mBuilder.setSound(null); // या चैनल पर setSound(null, null)
ऐप चलाएं, "Set Ringtone" दबाकर साउंड चुनें, फिर "Create Notification" — चुना हुआ साउंड बजेगा।