最后,我设法找到了自己的解决方案.以下是代码

NotificationManager mNotificationManager =

(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

if(notificationSoundUri != null){

// Changing Default mode of notification

notificationCompatBuilder.setDefaults(Notification.DEFAULT_VIBRATE);

// Creating an Audio Attribute

AudioAttributes audioAttributes = new AudioAttributes.Builder()

.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)

.setUsage(AudioAttributes.USAGE_ALARM)

.build();

// Creating Channel

NotificationChannel notificationChannel = new NotificationChannel(context.getString(R.string.channel_id_prayers),context.getString(R.string.channel_name_prayers),NotificationManager.IMPORTANCE_HIGH);

notificationChannel.setSound(notificationSoundUri,audioAttributes);

mNotificationManager.createNotificationChannel(notificationChannel);

}

}

mNotificationManager.notify(0, notificationCompatBuilder.build());

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐