gpt4 book ai didi

java - 通知播放默认声音而不是安装的声音

转载 作者:行者123 更新时间:2023-12-01 23:41:13 24 4
gpt4 key购买 nike

我正在尝试将 Uri 放在系统铃声之一上(用户自己选择)。然后这个 Uri 在通知中使用。但通知播放默认声音而不是用户选择的声音。这是打开用于选择声音的 Activity 的按钮代码

ringtone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
startActivityForResult(intent, 5);

在此之后我得到 URI

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 5){
uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
ringtone.setText(uri.toString());
}
}

例如,选择后的Uri如下所示:CONTENT://MEDIA/INTERNAL/AUDIO/MEDIA/36这是正确的 Uri 吗?

并且通过单击另一个按钮,应该会出现通知


music.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
Intent intent = new Intent();
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Resources res = context.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentIntent(contentIntent).setSmallIcon(R.drawable.play25x25).setContentTitle("Title").setContentText("Text").setSound(uri).setChannelId("1");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);;
NotificationChannel channel = new NotificationChannel("1", "name", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
notificationManager.notify(1, builder.build());
}
});

aa出现了,但是有默认的系统通知声音,不是用户安装的请帮忙。我做错了什么?我想,也许我需要 list 中的任何权限?

最佳答案

您是否在应用启动时创建通知 channel ?在App的onCreate()中。像这样

 channelId = getString(R.string.notification_chat_channel);
channel = new NotificationChannel(channelId,
"Some channel name",
NotificationManager.IMPORTANCE_DEFAULT);
Uri uri = Uri.parse("android.resource://" + this.getPackageName() + "/" + R.raw.push_sound);
channel.setSound(uri, att);
notificationManager.createNotificationChannel(channel);

关于java - 通知播放默认声音而不是安装的声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58258305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com