gpt4 book ai didi

android - 为通知设置自定义声音

转载 作者:行者123 更新时间:2023-12-03 02:35:22 27 4
gpt4 key购买 nike

I'm trying to set custom sound for notification.



详情:

我正在尝试根据用户的选择(从他们的手机存储中)设置通知声音。
为此,我将路径保存到 db 并在通知出现时显示通知并设置声音;我从现有通知 ID 中获取声音。

从那我决定哪个声音应该播放用户选择的声音或默认声音......但这根本不起作用......它不播放任何声音,甚至不是默认声音。

如何设置/播放自定义声音通知!?

经过谷歌研究:许多建议从原始文件夹播放声音,但我没有得到答案,我将用户选择的声音保存到原始文件夹(当然是编程盟友),所以我可以从原始文件夹播放声音..

PS 有很多与此相关的答案,但它们不符合我的要求

这是我的代码:
 private void showNotification(Context context, Things things) {

try {
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon)
//example for large icon
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.icon))
.setContentTitle(things.getTitle())
.setContentText(things.getThing()).setSubText(things.getNotification())
.setOngoing(false)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true);
Intent i = new Intent(context, HomeActivity.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(
context,
0,
i,
PendingIntent.FLAG_ONE_SHOT
);
// example for blinking LED
mBuilder.setLights(0xFFb71c1c, 1000, 2000);
if (things.getRingtone() == null || things.getRingtone().equals("")) {
mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

} else {
Ringtone r = RingtoneManager.getRingtone(context, Uri.parse(things.getRingtone()));
r.play();
Toast.makeText(context, things.getRingtone(), Toast.LENGTH_SHORT).show();
}
//mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
mBuilder.setContentIntent(pendingIntent);
mNotifyMgr.notify(12345, mBuilder.build());
} catch (Exception e) {
Log.d(getClass().getName(), "catch " + e.toString());
}
}

最佳答案

从 API 26 (Oreo) 开始,您需要在通知 channel 上设置声音。 channel 一旦创建,其设置(包括声音)将无法更改,除非重新安装应用程序或清除本地数据。

因此,最好的方法是拥有两个(或更多) channel - 一个用于您想要播放的每种声音/振动。

在代码中,您可以根据要播放的声音来决定使用哪个 channel 。

这是我的代码,我根据客户端的设置播放默认通知声音或自定义声音。该代码还负责在 API 26 之前运行 Android 的设备:

String sound = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("NotificationsSound", getString(R.string.settingsNotificationSiren));
Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+ getApplicationContext().getPackageName() + "/" + R.raw.siren);
NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel mChannel;
String channel_id = Utils.CHANNEL_DEFAULT_ID;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (sound.toLowerCase().equals(getString(R.string.settingsNotificationSiren).toLowerCase())) {
channel_id = Utils.CHANNEL_SIREN_ID;
mChannel = new NotificationChannel(Utils.CHANNEL_SIREN_ID, Utils.CHANNEL_SIREN_NAME, NotificationManager.IMPORTANCE_HIGH);
mChannel.setLightColor(Color.GRAY);
mChannel.enableLights(true);
mChannel.setDescription(Utils.CHANNEL_SIREN_DESCRIPTION);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
mChannel.setSound(soundUri, audioAttributes);
} else {
mChannel = new NotificationChannel(Utils.CHANNEL_DEFAULT_ID, Utils.CHANNEL_DEFAULT_NAME, NotificationManager.IMPORTANCE_HIGH);
mChannel.setLightColor(Color.GRAY);
mChannel.enableLights(true);
mChannel.setDescription(Utils.CHANNEL_DEFAULT_DESCRIPTION);
}
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannel( mChannel );
}
}

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channel_id)
.setSmallIcon(R.drawable.ic_stat_maps_local_library)
.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.ic_launcher))
.setTicker(title)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setAutoCancel(true)
.setLights(0xff0000ff, 300, 1000) // blue color
.setWhen(System.currentTimeMillis())
.setPriority(NotificationCompat.PRIORITY_DEFAULT);

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
if (sound.toLowerCase().equals(getString(R.string.settingsNotificationSiren).toLowerCase())) {
mBuilder.setSound(soundUri);
} else {
mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
}
}

int NOTIFICATION_ID = 1; // Causes to update the same notification over and over again.
if (mNotificationManager != null) {
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

关于android - 为通知设置自定义声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45956939/

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