奥利奥+通知声音问题



我正在尝试为通知设置自定义声音。问题是它总是在 res/raw 文件夹中播放第一个声音,无论那里有多少文件或我试图更改多少 uri。如果我从原始文件夹中删除所有文件,则根本不播放任何声音。在Android 6上,它运行良好。

我希望能够设置来自外部/内部存储的声音以及系统声音。可能吗?

这是我的代码:

notificationSoundUri = GeneralSettingsManager.getSoundForNotification(getApplicationContext(), site, pushType);
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, Splash.class);
notificationIntent.setAction(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
int smallIcon = R.drawable.big_green_v;
int backgroundColor = 0x8D1919;
String channelId = "default2";
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(smallIcon)
.setColor(backgroundColor)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setDefaults(Notification.DEFAULT_VIBRATE);

if(TextUtils.isEmpty(notificationSoundUri))
{
Timber.e("NOTIFICATION SOUND * MISSING");
mBuilder.setVibrate(new long[]{0L});
}
else
{
Timber.e("NOTIFICATION SOUND * " + notificationSoundUri);
mBuilder.setSound(Uri.parse(notificationSoundUri));
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
if(!TextUtils.isEmpty(notificationSoundUri))
{
// Create an Audio Attribute
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
//remove old channel
try {
mNotificationManager.deleteNotificationChannel(channelId);
}
catch (Exception e)
{
//do nothing
}

// Create new channel
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelId, NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setSound(Uri.parse(notificationSoundUri), audioAttributes);
mNotificationManager.createNotificationChannel(notificationChannel);
}
}
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

编辑: 上面的代码来自FirebaseMessagingService。用户将从外部存储或系统声音列表中选择所需的通知声音,并且应在显示通知时播放此声音。

是的,你可以!你必须使用铃声管理器(用于系统声音(,

Uri ringSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.sound = ringSound;

用于外部/内部 您需要查找特定声音文件的 URI。

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, 2);

处理响应

@Override
protected void onActivityResult(final int requestCode, final int resultCode, 
final Intent intent)
{
if (resultCode == Activity.RESULT_OK && requestCode == 2)
{
Uri uri = Intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (uri != null)
{
this.chosenRingtone = uri.toString();
}
else
{
this.chosenRingtone = null;
}
}            

}

最新更新