我正在尝试为通知播放自定义声音,我会搜索很多,但是运气不好,这是我的通知代码。
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
Notification notification;
mBuilder = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound )
.setStyle(inboxStyle)
.setNumber(1)
.setColor(mContext.getResources().getColor(R.color.blue))
.setChannelId("my_channel_01")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.app_icon)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message);
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String CHANNEL_ID = "my_channel_01";// The id of the channel.
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
CharSequence name = mContext.getString(R.string.app_name);// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setShowBadge(true);
mChannel.setSound(alarmSound,audioAttributes);
notificationManager.createNotificationChannel(mChannel);
notificationManager.notify(Config.NOTIFICATION_ID, mBuilder.build());
}
出现通知,但没有声音。