美文网首页
Android发送个普通自定义常驻通知

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

作者: 鸡蛋绝缘体 | 来源:发表于2022-09-28 11:13 被阅读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)
            }
        }
    
    

    相关文章

      网友评论

          本文标题:Android发送个普通自定义常驻通知

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