在 Android 中修改传入通知的声音
Modify sound of incoming Notification in Android
我已经设置了一个 NotificationListenerService
来监听通知。我需要修改以下方法中捕获的通知的 sound/alert 音调:
override fun onNotificationPosted(sbn: StatusBarNotification?) {
super.onNotificationPosted(sbn)
// Modify the tone here and notify ( the notification ) it again
}
到目前为止我尝试过的(不播放声音):
notification.notification.defaults = android.app.Notification.DEFAULT_VIBRATE
notification.notification.sound = Uri.parse(sharedPreferences.getString(getString( R.string.ringtone_key ) , Settings.System.DEFAULT_NOTIFICATION_URI.toString() ))
manager.notify( RECREATE_NOTIFICATION_ID , notification.notification )
我的问题在这里:
How can I modify the sound/alert tone of the StatusBarNotification
caught in the above method and display it to the user? Do I need to repost/recreate it again?
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
if (soundUri != null)
{
// Changing Default mode of notification
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE)
// Creating an Audio Attribute
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
// Creating Channel
val notificationChannel = NotificationChannel("CH_ID", "Testing_Audio", NotificationManager.IMPORTANCE_HIGH)
notificationChannel.setSound(soundUri, audioAttributes)
mNotificationManager.createNotificationChannel(notificationChannel)
}
}
要为 ismail alaoui 的回答添加一些上下文 - 你所做的可能应该适用于 pre-Oreo android 设备,但对于 Oreo 及更高版本,你需要创建一个通知渠道,将分配自定义声音。参考https://developer.android.com/guide/topics/ui/notifiers/notifications.
另请记住,用户可能会随时更改通知频道的声音:)
那么剩下的唯一问题 - 您在哪个 Android 版本上测试您的解决方案?
我已经设置了一个 NotificationListenerService
来监听通知。我需要修改以下方法中捕获的通知的 sound/alert 音调:
override fun onNotificationPosted(sbn: StatusBarNotification?) {
super.onNotificationPosted(sbn)
// Modify the tone here and notify ( the notification ) it again
}
到目前为止我尝试过的(不播放声音):
notification.notification.defaults = android.app.Notification.DEFAULT_VIBRATE
notification.notification.sound = Uri.parse(sharedPreferences.getString(getString( R.string.ringtone_key ) , Settings.System.DEFAULT_NOTIFICATION_URI.toString() ))
manager.notify( RECREATE_NOTIFICATION_ID , notification.notification )
我的问题在这里:
How can I modify the sound/alert tone of the
StatusBarNotification
caught in the above method and display it to the user? Do I need to repost/recreate it again?
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
if (soundUri != null)
{
// Changing Default mode of notification
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE)
// Creating an Audio Attribute
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
// Creating Channel
val notificationChannel = NotificationChannel("CH_ID", "Testing_Audio", NotificationManager.IMPORTANCE_HIGH)
notificationChannel.setSound(soundUri, audioAttributes)
mNotificationManager.createNotificationChannel(notificationChannel)
}
}
要为 ismail alaoui 的回答添加一些上下文 - 你所做的可能应该适用于 pre-Oreo android 设备,但对于 Oreo 及更高版本,你需要创建一个通知渠道,将分配自定义声音。参考https://developer.android.com/guide/topics/ui/notifiers/notifications.
另请记住,用户可能会随时更改通知频道的声音:)
那么剩下的唯一问题 - 您在哪个 Android 版本上测试您的解决方案?