Android 8.0+ (API 26+) में कस्टम नोटिफिकेशन साउंड
एंड्रॉइड ओरियो से नोटिफिकेशन चैनल में setSound() के साथ AudioAttributes सेट करना आवश्यक है।
चरण 1-3: प्रोजेक्ट, लेआउट, res/raw में साउंड फाइल (पिछले आर्टिकल के समान)
चरण 4: MainActivity.java
package app.tutorialspoint.com.notifyme;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
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 {
public static final String NOTIFICATION_CHANNEL_ID = "10001";
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 sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
+ getPackageName() + "/raw/quite_impressed.mp3");
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("कस्टम साउंड टेस्ट")
.setSound(sound)
.setContentText("कस्टम नोटिफिकेशन साउंड के साथ")
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION) // नोटिफिकेशन के लिए USAGE_NOTIFICATION बेहतर
.build();
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID, "कस्टम साउंड चैनल", importance);
channel.enableLights(true);
channel.setLightColor(Color.BLUE);
channel.enableVibration(true);
channel.setSound(sound, audioAttributes); // चैनल लेवल पर साउंड सेट करें
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
assert manager != null;
manager.createNotificationChannel(channel);
}
assert manager != null;
manager.notify((int) System.currentTimeMillis(), builder.build());
});
}
}
मुख्य अंतर (Pre-Oreo vs Oreo+)
| वर्जन | साउंड सेटिंग |
|---|---|
| Android 7.1 तक | Builder पर .setSound(uri) सीधे काम करता है |
| Android 8.0+ | चैनल पर channel.setSound(uri, audioAttributes) सेट करना जरूरी; Builder का setSound() इग्नोर होता है |
AudioAttributes उपयोग
CONTENT_TYPE_SONIFICATION- UI साउंड्स के लिएUSAGE_NOTIFICATION- नोटिफिकेशन साउंड (डिफ़ॉल्ट वॉल्यूम कंट्रोल से कंट्रोल होता है)USAGE_ALARM- अलार्म (डू नॉट डिस्टर्ब में भी बज सकता है)
साउंड फाइल फॉर्मेट
समर्थित: .mp3, .wav, .ogg। फाइल res/raw/ में रखें।