Reputation:
so I made a simple app of how notification work on android I use NotificationManager for making the simple notification app is work with setOnclick and I use PendingIntent.
I was run my app and is not show anything, I got some bug in this case
this is my MainActivity.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)
}
}
}
}
}
this is my main_activity.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:layout_centerHorizontal="true"
android:text="Show Notification" />
</RelativeLayout>
this is my logcat :
kotlin.UninitializedPropertyAccessException: lateinit property builder has not been initialized
at com.example.notiffication.MainActivity.getBuilder(MainActivity.kt:20)
at com.example.notiffication.MainActivity$onCreate$1.onClick(MainActivity.kt:53)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28296)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
and for the method Notification.Builder
I use old version
Upvotes: 0
Views: 104
Reputation: 2966
Now the problem is that you are calling the notify
method before build the notification.
Your activity should looks like this
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)
}
}
}
You can learn more about notifications here:
Upvotes: 2