[英]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.