@RequiresApi(api=Build.VERSION\u code.O)
私有void createChannels(){
Nmanager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
ArrayList arrayList = new ArrayList<String>();
Field[] fields = R.raw.class.getFields();
for (int i = 1; i < fields.length - 1; i++) {
arrayList.add(fields[i].getName());
}
DBRingtone RDB = new DBRingtone(this);
Cursor cursor = RDB.getringtone();
if (cursor.getCount() > 0) {
int ring = parseInt(cursor.getString(0));
resID = getResources().getIdentifier((String) arrayList.get(ring), "raw", getPackageName());
i=i+resID;
uri = Uri.parse("android.resource://" + getPackageName() + "/" + resID);
} else
{
i=i+10;
uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.default_sound);
}
CHANNEL_ID = CHANNEL_ID+i;
notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.enableVibration(true);
notificationChannel.setDescription("Your message");
notificationChannel.setLightColor(Color.RED);
notificationChannel.setSound(uri, attributes);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
Nmanager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Nmanager.createNotificationChannel(notificationChannel);
}
大家好,我已经用上面的代码解决了这个问题,它正在工作。您还可以将CHANNEL\u ID=CHANNEL\u ID+resID直接分配给。为此,我使用变量i进行了赋值。我存储了用户首选的通知声音resID is SQLite数据库,并在createchannels类中使用游标检索该resID以创建uri的路径。希望这能帮助你谢谢你。。。