【问题标题】:No custom sound in Notification in Kotlin Android 10Kotlin Android 10 的通知中没有自定义声音
【发布时间】: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


    【解决方案1】:

    我找到了更好的方法!!! 玩得开心:

     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_HIGH
                } else {
                    NotificationManager.IMPORTANCE_LOW
                }
                val audioAttribute = AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build()
                val soundUri = Uri.Builder().scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
                    .authority("blablabla")
                    .path(R.raw.i_feel_good.toString()).build()
                val notyChannel =
                    NotificationChannel(channelID, channelName, channelImportance)
    
                notyChannel.enableLights(true)
                notyChannel.lightColor = Color.RED
                notyChannel.enableVibration(true)
                notyChannel.vibrationPattern = longArrayOf(100, 1000, 100, 1000, 100,1000,100)
                notyChannel.setSound(soundUri,audioAttribute)
                this.createNotificationChannel(notyChannel)
            }
    

    【讨论】:

      【解决方案2】:

      而不是将声音更改为 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) ;在我的情况下添加Action-> .addAction(R.drawable.ic_start, "开始", playMusicPendingIntent)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-26
        • 1970-01-01
        • 2020-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多