Android发送个普通自定义常驻通知

2022-09-28  本文已影响0人  鸡蛋绝缘体
  private fun createNotificationChannels() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notificationManager =
                getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            val channelName = getString(R.string.app_name)
            val descriptionText = getString(R.string.channel_description)
            val channel = NotificationChannel(
                NotificationID.CHANNEL_ID,
                channelName,
                NotificationManager.IMPORTANCE_MIN
            ).apply {
                description = descriptionText
            }
            channel.enableLights(false)
            channel.enableVibration(false)
            channel.setSound(null, null)
            channel.setShowBadge(false)
            notificationManager.createNotificationChannel(channel)
        }
    }

    fun createNotification() {
        val notificationCustomView = notificationCustomView(R.layout.notification_show)
        val notification = NotificationCompat.Builder(this, NotificationID.CHANNEL_ID)
            .setSmallIcon(R.mipmap.icon)
            .setLargeIcon( BitmapFactory.decodeResource(
                resources,
                R.mipmap.icon
            ))
            .setCustomBigContentView(notificationCustomView)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setGroupSummary(false)
            .setGroup( getString(R.string.app_name))
            .setContent(notificationCustomView)
            .setAutoCancel(false)
            .setSilent(true)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setOngoing(true) //通知栏常驻
            .build()

        with(NotificationManagerCompat.from(this)) {
            notify(NotificationID.NOTIFICATION_ID, notification)
        }
    }

上一篇下一篇

猜你喜欢

热点阅读