美文网首页
Android通知Notification设置setSound无

Android通知Notification设置setSound无

作者: kevinsEegets | 来源:发表于2020-09-15 10:21 被阅读0次
    u=1678011113,2635959946&fm=26&gp=0.jpg

    对于大于等于API 26 [Build.VERSION.SDK_INT >= Build.VERSION_CODES.O],您需要在通知通道上设置声音

    
    fun createNotifyChannel(context: Context): String? {
    
        val sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.packageName + "/raw/" + R.raw.sound)
        // NotificationChannels are required for Notifications on O (API 26) and above.
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    
            val audioAttributes = AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build()
    
            // Initializes NotificationChannel.
            val notificationChannel = NotificationChannel("huawei", "Sample Social", NotificationManager.IMPORTANCE_DEFAULT)
            notificationChannel.enableVibration(true) //是否有震动
            notificationChannel.lockscreenVisibility = notifPublicVisible()
    
            notificationChannel.setSound(sound, audioAttributes)
            // Adds NotificationChannel to system. Attempting to create an existing notification
            // channel with its original values performs no operation, so it's safe to perform the
            // below sequence.
            val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(notificationChannel)
            "huawei"
        } else {
            // Returns null for pre-O (26) devices.
            null
        }
    }
    

    对于小于API 26 [Build.VERSION.SDK_INT < Build.VERSION_CODES.O],在NotificationCompat.Builder设置声音

    val sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + packageName + "/raw/" + R.raw.sound)
    NotificationCompat.Builder(context, channelId)
    .setSound(sound)
    .build()
    

    最后尝试清除数据(或全新安装)
    再试一次

    参考自:https://www.thinbug.com/q/48986856

    相关文章

      网友评论

          本文标题:Android通知Notification设置setSound无

          本文链接:https://www.haomeiwen.com/subject/ojbxyktx.html