我有一个简单的通知应用程序,我试图在其中添加 mp3 声音作为通知声音。每次触发通知时,都会播放默认通知,而不是原始文件夹中的 mp3 文件。我尝试了很多并使用了很多解决方案,但仍然无法正常工作。
这是我在按钮单击表单 MainActivity 上的代码;
binding.btnShowNotifications.setOnClickListener(v -> {
Toast.makeText(this, "Set reminder", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
long system = System.currentTimeMillis();
long ten = 10 * 1000;
alarmManager.set(AlarmManager.RTC_WAKEUP, system + ten, pendingIntent);
});
这是我的广播接收器类;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
import androidx.core.app.NotificationCompat;
public class NotificationReceiver extends BroadcastReceiver {
public static final String CHANNEL_ONE_ID = "channel_one";
@Override
public void onReceive(Context context, Intent intent) {
Uri mediaPath = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.sound_one);
createNotifications(context, mediaPath);
}
private void createNotifications(Context context, Uri mediaPath) {
try {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channel = new NotificationChannel(
CHANNEL_ONE_ID,
"Test Channel",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setLightColor(Color.GRAY);
channel.enableLights(true);
channel.enableVibration(true);
channel.setDescription(context.getResources().getString(R.string.app_name));
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
channel.setSound(mediaPath, audioAttributes);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
NotificationCompat.Builder mNotification = new NotificationCompat.Builder(context, CHANNEL_ONE_ID);
mNotification
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle(context.getResources().getString(R.string.app_name))
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_DEFAULT)
.setVibrate(new long[]{0, 500, 1000})
.setDefaults(Notification.DEFAULT_LIGHTS)
.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.sound_one))
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentText(context.getResources().getString(R.string.app_name));
assert notificationManager != null;
notificationManager.notify(1, mNotification.build());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}