[英]Android notifications is not showing
测试通知未显示。 使用 Android 工作室和 Kotlin。
class MainActivity : AppCompatActivity() {
companion object {
const val NOTIFICATION_ID = 101
const val CHANNEL_ID = "channelID"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button : Button = findViewById(R.id.button_notification)
button.setOnClickListener {
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Notification")
.setContentText("Go ahead")
.setPriority(Notification.PRIORITY_HIGH)
.setAutoCancel(true)
val notificationManager = NotificationManagerCompat.from(this)
notificationManager.notify(NOTIFICATION_ID, builder.build())
}
}
}
build.gradle(模块级别)
....
dependencies {
implementation 'androidx.core:core:1.9.0'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.6.0'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
当运行应用程序并按下按钮时 - 没有任何反应,没有任何通知。
在模块级别 build.gradle 中添加了implementation 'androidx.core:core:1.9.0'
从 Android 8.0 开始,你需要创建一个NotificationChannel
才能真正从你的 App 发布一个 Notification。 更多细节你可以参考这个。
因为您必须在 Android 8.0 及更高版本上发布任何通知之前创建通知通道,所以您应该在应用启动后立即执行此代码。 重复调用此方法是安全的,因为创建现有通知通道不会执行任何操作。
所以,在创建NotificationChannel
之前需要添加一个function来创建NotificationChannel:
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.btnNotification).setOnClickListener {
createNotificationChannel()
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Notification from Test")
.setContentText("Test Notification Message received!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager // NotificationManagerCompat.from(this)
notificationManager.notify(NOTIFICATION_ID, builder.build())
}
}
private fun createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
val name = "TestChannel"
val descriptionText = "TestDescription"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(CHANNEL_ID, name, importance)
channel.description = descriptionText
// Register the channel with the system
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
companion object {
const val NOTIFICATION_ID = 101
const val CHANNEL_ID = "channelID"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.