在本地通知中设置自定义声音

4

Android 13

使用自定义声音创建本地通知

我在res/raw/event_sound.mp3中有我的event_sound.mp3音频文件

我有以下通知

val notification = NotificationCompat.Builder(context, channelId)
    .setContentTitle(title)
    .setContentText(description)
    .setSmallIcon(R.drawable.bell)
    .setAutoCancel(true)
    .setContentIntent(pendingIntent)
    .setSound(getUriSoundFile(context))
    .build()

我有以下方法来提取我的数据模块中的音频文件。
 private fun getUriSoundFile(context: Context): Uri {
        val uri = Uri.parse("android.resource://" + "me.androidbox.data" + "/raw/" + "event_sound.mp3")

        return uri
    }

当我检查完整路径时,我得到以下结果:
android.resource://me.androidbox.data/raw/event_sound.mp3

我已经在我的Application类中设置了我的频道,就像这样:
  private fun createNotificationChannel(listOfChannel: Map<String, String>) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            listOfChannel.map { mapOfChannel ->
                val notificationManager = getNotificationManager()
                val notificationChannel = NotificationChannel(
                    mapOfChannel.key,
                    mapOfChannel.value,
                    NotificationManager.IMPORTANCE_HIGH
                )

                notificationManager.createNotificationChannel(notificationChannel)
            }
        }
    }

然而,通知只会播放默认的声音,从不播放我的自定义声音。
更新====
我在我的Application类中有以下内容。event_sound直接在以下位置。

raw/event_sound.mp3

这是调试输出: android.resource://me.androidbox.presentation/2131558400 这是我的更新代码:
private fun createNotificationChannel(listOfChannel: Map<String, String>) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        listOfChannel.map { mapOfChannel ->
            val notificationManager = getNotificationManager()
            val notificationChannel = NotificationChannel(
                mapOfChannel.key,
                mapOfChannel.value,
                NotificationManager.IMPORTANCE_HIGH
            )

            val audioAttributes = AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
                .build()

            notificationChannel.setSound(getUriSoundFile(), audioAttributes)
            notificationManager.createNotificationChannel(notificationChannel)
        }
    }
}
2个回答

6
问题描述:在Android 13之后,无论应用程序选择的通知声音如何,都会被设置为默认声音。
请确保您的通知声音以mp3格式保存在res/raw目录下。
您可以将else部分保持不变。
新答案
private fun getUriSoundFile(context: Context): Uri {
        val uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.packageName + "/" + R.raw.event_sound.mp3)

        return uri
    }


private fun createNotificationChannel(listOfChannel: Map<String, String>) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        listOfChannel.map { mapOfChannel ->
            val notificationManager = getNotificationManager()
            val notificationChannel = NotificationChannel(
                mapOfChannel.key,
                mapOfChannel.value,
                NotificationManager.IMPORTANCE_HIGH
            )

            val audioAttributes = AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build()

            notificationChannel.setSound(getUriSoundFile(), audioAttributes)
            notificationManager.createNotificationChannel(notificationChannel)
        }
    }
}

旧答案

private fun createNotificationChannel() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val audioAttributes = AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
                    .build()
    
                val uri = Uri.parse("android.resource://$packageName/${R.raw.sample_sound}")
    
                val channel = NotificationChannel(
                    CHANNEL_ID,
                    "Channel Name",
                    NotificationManager.IMPORTANCE_DEFAULT
                ).apply {
                    description = "Description"
                    setSound(uri, audioAttributes)
                }
                val notificationManager =
                    getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                notificationManager.createNotificationChannel(channel)
            }
        }

请分享listOfChannel参数的值。 - Arda Kazancı
谢谢。以上代码在Android 11设备上工作正常。然而,我有一个Android 13设备,以上代码不起作用。根据您的问题描述,我想知道这是否意味着在Android 13上我们无法更改通知声音? - ant2009
1
从技术上讲,我们使用所有的声道来调整它们的声音。在Android 13中,我认为可能需要手动管理它。我认为他们将来会修复这个问题。 - Arda Kazancı
1
从技术上讲,我们使用所有的声道来调整它们的声音。在Android 13中,我认为可能需要手动管理它。我认为他们将来会修复这个问题。 - Arda Kazancı
1
从技术上讲,我们使用所有的声道来调整它们的声音。在Android 13中,我认为可能需要手动管理它。我认为他们将来会修复这个问题。 - undefined
显示剩余10条评论

1

这里有一个解决了相同问题的问题,此外我还附上了参考链接 Android notification setSound is not working

String CHANNEL_ID="1234";

    Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+ getApplicationContext().getPackageName() + "/" + R.raw.mysound);
    NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 
    
  //For API 26+ you need to put some additional code like below:
    NotificationChannel mChannel;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                mChannel = new NotificationChannel(CHANNEL_ID, Utils.CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
                mChannel.setLightColor(Color.GRAY);
                mChannel.enableLights(true);
                mChannel.setDescription(Utils.CHANNEL_SIREN_DESCRIPTION);
                AudioAttributes audioAttributes = new AudioAttributes.Builder()
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                        .build();
                mChannel.setSound(soundUri, audioAttributes);
    
                if (mNotificationManager != null) {
                    mNotificationManager.createNotificationChannel( mChannel );
                }
        }

   //General code:
     NotificationCompat.Builder status = new NotificationCompat.Builder(getApplicationContext(),CHANNEL_ID);     
                          status.setAutoCancel(true)
                                .setWhen(System.currentTimeMillis())
                                .setSmallIcon(R.drawable.logo)
                                //.setOnlyAlertOnce(true)
                                .setContentTitle(getString(R.string.app_name))
                                .setContentText(messageBody)
                                .setVibrate(new long[]{0, 500, 1000})
                                .setDefaults(Notification.DEFAULT_LIGHTS )
                                .setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+ "://" +mContext.getPackageName()+"/"+R.raw.apple_ring))
                                .setContentIntent(pendingIntent)
                                .setContent(views);
                        
                        mNotificationManager.notify(major_id, status.build());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接