【发布时间】:2020-03-28 18:36:52
【问题描述】:
我试图播放自定义声音文件作为通知,但手机只播放默认声音文件。(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)
}
}
}
}
【问题讨论】:
标签: kotlin audio timer notifications foreground