通知栏未在android中显示

通知栏未在android中显示,android,xml,android-studio,kotlin,android-notifications,Android,Xml,Android Studio,Kotlin,Android Notifications,因此,我制作了一个简单的应用程序,介绍了通知如何在android上工作。我使用NotificationManager制作了一个简单的通知应用程序,它与setOnclick一起工作,我使用PendingContent 我正在运行我的应用程序,但没有显示任何内容,在这种情况下,我遇到了一些错误 这是我的主要活动.kt: import android.app.Notification import android.app.NotificationChannel import a

因此,我制作了一个简单的应用程序,介绍了通知如何在android上工作。我使用NotificationManager制作了一个简单的通知应用程序,它与setOnclick一起工作,我使用PendingContent

我正在运行我的应用程序,但没有显示任何内容,在这种情况下,我遇到了一些错误

这是我的主要活动.kt:

    import android.app.Notification
    import android.app.NotificationChannel
    import android.app.NotificationManager
    import android.app.PendingIntent
    import android.content.Context
    import android.content.Intent
    import android.graphics.Color
    import android.os.Build
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.widget.Button
    import kotlinx.android.synthetic.main.activity_main.*
    
    class MainActivity : AppCompatActivity() {
    
        lateinit var notificationManager: NotificationManager
        lateinit var notificationChannel: NotificationChannel
        lateinit var builder: Notification.Builder
    
        val channelId = "com.example.notiffication"
    
        val description = "My Notification"
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            val show = findViewById<Button>(R.id.btn_show)
    
            notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            show.setOnClickListener {
    
    
                val intent = Intent()
    
                val pendingIntent =
                    PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    
    
    
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    notificationChannel =
                        NotificationChannel(channelId, description, NotificationManager.IMPORTANCE_HIGH)
    
                    notificationChannel.enableLights(true)
                    notificationChannel.lightColor = Color.RED
                    notificationChannel.enableVibration(true)
  notificationManager.notify(0, builder.build())
    
                    notificationManager.createNotificationChannel(notificationChannel)
    
                    builder = Notification.Builder(this, channelId)
                        .setContentTitle("Android")
                        .setContentText("New Message")
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentIntent(pendingIntent)
    
                } else {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        builder = Notification.Builder(this)
                            .setContentTitle("Android")
                            .setContentText("New Message")
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentIntent(pendingIntent)
                    }
              
                }
    
            }
    
        }
    }

对于方法
Notification.Builder
我使用旧版本现在的问题是,您在生成通知之前调用了
notify
方法

您的活动应该如下所示

class MainActivity : AppCompatActivity() {

    companion object {
        private const val CHANNEL_ID = "default_channel"
        private const val CHANNEL_NAME = "My Notification"
        private const val NOTIFICATION_ID = 123
    }

    private val notificationManager by lazy {
        getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val btnShow = findViewById<Button>(R.id.btn_show)
        btnShow.setOnClickListener {
            createChannel()
            val notification = buildNotification()
            notificationManager.notify(NOTIFICATION_ID, notification)
        }
    }

    private fun buildNotification(): Notification {
        val intent = Intent()
        val pendingIntent = PendingIntent
            .getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
        return NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("Android")
            .setContentText("New Message")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent)
            .build()
    }

    private fun createChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel =
                NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH)
            channel.enableLights(true)
            channel.lightColor = Color.RED
            channel.enableVibration(true)
            notificationManager.createNotificationChannel(channel)
        }
    }
    
}
class MainActivity:AppCompatActivity(){
伴星{
专用const val CHANNEL\u ID=“默认\u CHANNEL”
private const val CHANNEL_NAME=“我的通知”
私有const val通知_ID=123
}
lazy提供的私有val notificationManager{
getSystemService(Context.NOTIFICATION\u服务)作为NotificationManager
}
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnShow=findViewById(R.id.btn_show)
btnShow.setOnClickListener{
createChannel()
val notification=buildNotification()
notificationManager.notify(通知ID,通知)
}
}
private fun buildNotification():通知{
val intent=intent()
val pendingent=pendingent
.getActivity(this,0,intent,pendingent.FLAG\u UPDATE\u CURRENT)
返回NotificationCompat.Builder(此,通道ID)
.setContentTitle(“Android”)
.setContentText(“新消息”)
.setSmallIcon(R.mipmap.ic_启动器)
.setContentIntent(挂起内容)
.build()
}
私人娱乐频道(){
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.O){
瓦尔通道=
NotificationChannel(频道ID、频道名称、NotificationManager.重要性高)
通道.启用灯(真)
channel.lightColor=Color.RED
通道启用振动(真)
notificationManager.createNotificationChannel(频道)
}
}
}
您可以在此处了解有关通知的更多信息:


现在的问题是,您在生成通知之前调用了
notify
方法

您的活动应该如下所示

class MainActivity : AppCompatActivity() {

    companion object {
        private const val CHANNEL_ID = "default_channel"
        private const val CHANNEL_NAME = "My Notification"
        private const val NOTIFICATION_ID = 123
    }

    private val notificationManager by lazy {
        getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val btnShow = findViewById<Button>(R.id.btn_show)
        btnShow.setOnClickListener {
            createChannel()
            val notification = buildNotification()
            notificationManager.notify(NOTIFICATION_ID, notification)
        }
    }

    private fun buildNotification(): Notification {
        val intent = Intent()
        val pendingIntent = PendingIntent
            .getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
        return NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("Android")
            .setContentText("New Message")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent)
            .build()
    }

    private fun createChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel =
                NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH)
            channel.enableLights(true)
            channel.lightColor = Color.RED
            channel.enableVibration(true)
            notificationManager.createNotificationChannel(channel)
        }
    }
    
}
class MainActivity:AppCompatActivity(){
伴星{
专用const val CHANNEL\u ID=“默认\u CHANNEL”
private const val CHANNEL_NAME=“我的通知”
私有const val通知_ID=123
}
lazy提供的私有val notificationManager{
getSystemService(Context.NOTIFICATION\u服务)作为NotificationManager
}
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnShow=findViewById(R.id.btn_show)
btnShow.setOnClickListener{
createChannel()
val notification=buildNotification()
notificationManager.notify(通知ID,通知)
}
}
private fun buildNotification():通知{
val intent=intent()
val pendingent=pendingent
.getActivity(this,0,intent,pendingent.FLAG\u UPDATE\u CURRENT)
返回NotificationCompat.Builder(此,通道ID)
.setContentTitle(“Android”)
.setContentText(“新消息”)
.setSmallIcon(R.mipmap.ic_启动器)
.setContentIntent(挂起内容)
.build()
}
私人娱乐频道(){
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.O){
瓦尔通道=
NotificationChannel(频道ID、频道名称、NotificationManager.重要性高)
通道.启用灯(真)
channel.lightColor=Color.RED
通道启用振动(真)
notificationManager.createNotificationChannel(频道)
}
}
}
您可以在此处了解有关通知的更多信息:


旁注:两个ifs都有相同的条件
Build.VERSION.SDK\u INT>=Build.VERSION\u code.O
这意味着什么,旁注:两个ifs都有相同的条件
Build.VERSION.SDK\u INT>=Build.VERSION\u code.O
这意味着什么,为什么我想使用这个代码请给我更多的解释,为了让人们理解。我希望这有助于你我对此有一个bug请再次查看我的答案请标记为已接受答案并投票如果这有助于你为什么我使用此代码请给我更多解释,为了让人们理解,我希望这对你有帮助。我有一个错误,请再次查看我的答案。请标记为已接受的答案,如果这对你有帮助,请进行投票
class MainActivity : AppCompatActivity() {

    companion object {
        private const val CHANNEL_ID = "default_channel"
        private const val CHANNEL_NAME = "My Notification"
        private const val NOTIFICATION_ID = 123
    }

    private val notificationManager by lazy {
        getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val btnShow = findViewById<Button>(R.id.btn_show)
        btnShow.setOnClickListener {
            createChannel()
            val notification = buildNotification()
            notificationManager.notify(NOTIFICATION_ID, notification)
        }
    }

    private fun buildNotification(): Notification {
        val intent = Intent()
        val pendingIntent = PendingIntent
            .getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
        return NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("Android")
            .setContentText("New Message")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent)
            .build()
    }

    private fun createChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel =
                NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH)
            channel.enableLights(true)
            channel.lightColor = Color.RED
            channel.enableVibration(true)
            notificationManager.createNotificationChannel(channel)
        }
    }
    
}