kotlin - Kotlin Android 10 的通知中没有自定义声音
问题描述
我试图播放自定义声音文件作为通知,但手机只播放默认声音文件。(Kotlin)我已经卸载并安装了该应用程序。代码应该没问题。该应用程序是一个倒计时到零的计时器。-> 现在零应该是一个声音(原始文件),我的代码:首先是我的函数。然后是 BasicNotificationBuilder,然后是 PendingIntentWithStack,最后是带有 createNotificationChannel 的 NotificationManager。我发现的都是Java。Kotlin 的解决方案非常适合运行声音文件 谢谢
fun showTimerExpired(context: Context) {
val startIntent = Intent(context, TimerNotificationActionReceiver::class.java)
startIntent.action = Constants.ACTION_START
val startPendingIntent = PendingIntent.getBroadcast(
context, 0, startIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val soundUri:Uri = Uri.parse(
"android.resource://" + context.packageName.toString()
+ "/" + q7.com.eieruhrbackforenot.R.raw.i_feel_good)
val notificationBuilder =
getBasicNotificationBuilder(context, CHANNEL_ID, true)
notificationBuilder.setContentTitle("Timer Expired")
.setContentText("Fertig")
.setContentIntent(getPendingIntentWithStack(context, MainActivity::class.java))
.addAction(q7.com.eieruhrbackforenot.R.drawable.ic_start, "Start", startPendingIntent)
.setSound(soundUri, AudioManager.STREAM_NOTIFICATION)
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(
CHANNEL_ID, NAME_CHANNEL_ID, true
)
notificationManager.notify(TIMER_ID, notificationBuilder.build())
}
private fun getBasicNotificationBuilder(
context: Context, channelID: String, playSound: Boolean
): NotificationCompat.Builder {
val soundUri:Uri = Uri.parse(
"android.resource://" + context.packageName.toString()
+ "/" + q7.com.eieruhrbackforenot.R.raw.i_feel_good)
val notificationBuilder = NotificationCompat.Builder(context, channelID)
.setSmallIcon(q7.com.eieruhrbackforenot.R.drawable.ic_timer)
.setAutoCancel(true)// User drückt Notification zum schliessen
.setDefaults(0)// keine Defaults
.setSound(soundUri)
if (playSound) {
notificationBuilder.setSound(soundUri)
//notificationBuilder.setSound(notificationSoundUri)
}
return notificationBuilder
}
private fun <T> getPendingIntentWithStack(
context: Context,
javaClass: Class<T>
): PendingIntent {
val soundUri:Uri = Uri.parse(
"android.resource://" + context.packageName.toString()
+ "/" + q7.com.eieruhrbackforenot.R.raw.i_feel_good)
val resultIntent = Intent(context, javaClass)
resultIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
val stackBuilder = TaskStackBuilder.create(context)
stackBuilder.addParentStack(javaClass)
stackBuilder.addNextIntent(resultIntent)
return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
}
private fun NotificationManager.createNotificationChannel(
channelID: String,
channelName: String,
playSound: Boolean
) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelImportance = if (playSound) {
NotificationManager.IMPORTANCE_DEFAULT
} else {
NotificationManager.IMPORTANCE_LOW
}
val notificationChannel =
NotificationChannel(channelID, channelName, channelImportance)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.CYAN
notificationChannel.enableVibration(true)
notificationChannel.vibrationPattern = longArrayOf(100,200,300,400,500,400,300,200,400)
this.createNotificationChannel(notificationChannel)
}
}
}
}
解决方案
而不是将声音更改为 mp3 文件。我创建了一个带有未决意图的意图,它在计时器结束时开始播放音乐。它有效,但这不是我想要的。
fun showTimerIsExpired(context: Context) {
val playMusicIntent = Intent(context,MainActivity::class.java)
playMusicIntent.action = MainActivity.musicStart().toString()
val playMusicPendingIntent = PendingIntent.getBroadcast(
context,0,playMusicIntent,PendingIntent.FLAG_UPDATE_CURRENT)
并将 pendingIntent 设置为 .ContentIntent-> .setContentIntent(playMusicPendingIntent) ;在我的情况下为 addAction-> .addAction(R.drawable.ic_start, "Start", playMusicPendingIntent)
推荐阅读
- java - 我的 ArrayList 两次打印同一个对象
- r - 是否可以在 R 中为时间序列设计执行多维分段泊松回归?
- android - 在运行时更改 BottomNavigationView 的图标
- javascript - React.js:元素类型无效:
- c - “void”是标量类型吗?
- java - 在 Android 中切换案例以将图像替换为另一个图像。像 Tinder Image 交换器之类的东西
- php - 我在 PHP 中使用 CSS 边框属性,但它不起作用
- node.js - 从超级账本结构 getStateByRange 和 getStateByPartialCompositeKey 获取响应时遇到问题
- android - 启用 Proguard 时 Gson 崩溃枚举字段
- java - executeUpdate 为“INSERT INTO with SELECT”返回不正确的值