所以我已经有一段时间了。我的通知根本没有发出任何声音,我尝试以编程方式将其更改为默认声音,但无法正常工作。我目前在Android Studio(Pixel 2XL Oreo)中使用Android模拟器。下面是代码,我已经启动了频道并在频道中设置了声音,但是它不起作用。
// Get the uri of the sound
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + mContext.getPackageName() + "/" + R.raw.kepingalertupsound);
// Get the Notification Manager using System Service for creating notification
NotificationManager notificationManager = (NotificationManager)
mContext.getSystemService(Context.NOTIFICATION_SERVICE);
// Create a notification channel to initiate notification
// Use High importance to pop-up display
// Check SDK version
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Creating Audio Attribute
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationChannel mChannel = new NotificationChannel(
KEPING_NOTIFICATION_CHANNEL_ID,
mContext.getString(R.string.alert_notification_channel_name),
NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel
mChannel.setDescription(notificationResult);
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setSound(sound, attributes);
if(notificationManager != null) {
notificationManager.createNotificationChannel(mChannel);
}
}
// Build the notification here
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(mContext, KEPING_NOTIFICATION_CHANNEL_ID)
.setColor(ContextCompat.getColor(mContext, R.color.colorPrimaryDark))
.setSmallIcon(R.drawable.ic_bitcoin_color)
.setLargeIcon(largeIcon(mContext))
.setContentTitle(mContext.getString(R.string.notification_alert_title))
.setContentText(notificationResult)
.setContentIntent(contentIntent(mContext))
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true);
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
} else {
notificationBuilder.setChannelId(KEPING_NOTIFICATION_CHANNEL_ID);
}
if(notificationManager != null) {
// Start the notification
notificationManager.notify(ALERT_NOTIFICATION_ID, notificationBuilder.build());
}
// Unregister Content Observer
mContext.getContentResolver().unregisterContentObserver(mContentObserver);