0
votes

I have written code for notification with notification channel. Its making default sound in my two vivo phones but its not at all making sound in my xiaomi phone with android os 7 and also in my samsung phone with android 8.

Here is my notification code:

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)
        }
    }

And here is the createChannel method code:

  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)
    }

I want to show notification with default notification sound of the device and vibration. I have tried setSound and setVibration also in notification builder, but its not working at all. Please tell me what my code is missing or is wrong.

1

1 Answers

0
votes

You can check the app notification sound permission granted or not from the device

Alow sound