Уведомление Android не издает звук и вибрацию

Я написал код для уведомления с каналом уведомления. Он издает звук по умолчанию на двух моих телефонах vivo, но совсем не звучит на моем телефоне xiaomi с ОС Android 7, а также на моем телефоне Samsung с Android 8.

Вот мой код уведомления:

fun showNotification(context: Context) {

        Log.e("xoxo", "showNotification called")

        val homeIntent = Intent(context, HomeActivity::class.java)
        homeIntent.putExtra(Constants.INTENT, 1)

        val pendingIntent = PendingIntent.getActivity(
            context, 0, homeIntent, PendingIntent.FLAG_UPDATE_CURRENT
        )

        var builder = NotificationCompat.Builder(context,CHANNEL_ID)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Hey")
            .setContentText("Lorem Ipsum is simply...")
            .setStyle(
                NotificationCompat.BigTextStyle().bigText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s ")
            )
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .setPriority(NotificationCompat.PRIORITY_HIGH)

        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager


        with(notificationManager) {
            createChannel(
                this,
                Constants.CHANNEL_ID,
                Constants.CHANNEL_NAME
            )

            // notificationId is a unique int for each notification that you must define

            var notification = builder.build()

            notify(Constants.NOTIF_ID, notification)
        }
    }

А вот код метода createChannel:

  fun createChannel(  notificationManager: NotificationManager, channelId: String, channelName:String) {
        if (Build.VERSION.SDK_INT < 26) {
            return
        }
        val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH)

        var att: AudioAttributes =  AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .build()

        channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION),att)
        channel.vibrationPattern = longArrayOf(500,500,500)
        channel.enableLights(true)
        channel.enableVibration(true)


        notificationManager.createNotificationChannel(channel)
    }

Я хочу показать уведомление со звуком уведомления устройства по умолчанию и вибрацией. Я пробовал setSound и setVibration также в построителе уведомлений, но он вообще не работает. Пожалуйста, скажите мне, что мой код отсутствует или является неправильным.


person Cassius    schedule 17.07.2020    source источник


Ответы (1)


Вы можете проверить разрешение звука уведомления приложения предоставлено или нет с устройства

Включить звук

person John Christopher    schedule 17.07.2020
comment
нужны разрешения на звук? для звука по умолчанию также? - person Cassius; 17.07.2020
comment
да для некоторых устройств! проверьте обновленный ответ со скриншотом - person John Christopher; 17.07.2020