Test notification not showing. Using Android studio and 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(Module level)
....
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'
}
When run app and press button - nothing happend, no any notification.
In Module level build.gradle was added implementation 'androidx.core:core:1.9.0'
CodePudding user response:
Starting from Android 8.0, you need to create a NotificationChannel
before you can really publish a Notification from your App. For more details you can refer to this.
Because you must create the notification channel before posting any notifications on Android 8.0 and higher, you should execute this code as soon as your app starts. It's safe to call this repeatedly because creating an existing notification channel performs no operation.
So, you need to add a function to create NotificationChannel
before you create a Notification:
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"
}
}