0

我已经编写了通知通道的通知代码。它在我的两部vivo手机中发出默认声音,但在我的带有android os 7的小米手机和带有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,但它根本不起作用。请告诉我我的代码丢失或错误。

4

1 回答 1

0

您可以从设备检查是否授予应用通知声音权限

低声

于 2020-07-17T17:33:35.517 回答