Android Push Notification not triggering

Task reminder app for android. The app should allow users to schedule tasks and receive reminders when the task’s deadline approaches. But Android Push Notification not triggering. Basically createNotificationChannel is not getting triggered.

Below is the complete code:

    import android.app.NotificationChannel
    import android.app.NotificationManager
    import android.content.Context
    import android.os.Build
    import android.os.Bundle
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import androidx.core.app.NotificationCompat
    import androidx.core.app.NotificationManagerCompat
    import kotlinx.android.synthetic.main.activity_main.*
    import java.text.SimpleDateFormat
    import java.util.*

    class MainActivity : AppCompatActivity() {

        private val tasks = mutableListOf<Task>()

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)

            // Button click listener to add a new task
            addTaskButton.setOnClickListener {
                addTask()
            }

            // Simulate some tasks for demonstration
            tasks.add(Task("Task 1", "Description 1", getDate(1)))
            tasks.add(Task("Task 2", "Description 2", getDate(2)))
            tasks.add(Task("Task 3", "Description 3", getDate(3)))

            // Show tasks in the list view
            showTasks()
        }

        private fun addTask() {
            val title = taskTitleEditText.text.toString()
            val description = taskDescriptionEditText.text.toString()
            val deadline = taskDeadlineEditText.text.toString()

            if (title.isNotEmpty() && deadline.isNotEmpty()) {
                val task = Task(title, description, deadline)
                tasks.add(task)
                showTasks()

                // Schedule a reminder for the task deadline
                scheduleReminder(task)

                // Clear input fields
                taskTitleEditText.text.clear()
                taskDescriptionEditText.text.clear()
                taskDeadlineEditText.text.clear()
            } else {
                Toast.makeText(this, "Please enter title and deadline for the task", Toast.LENGTH_SHORT).show()
            }
        }

        private fun showTasks() {
            val adapter = TaskAdapter(this, tasks)
            taskListView.adapter = adapter
        }

        private fun scheduleReminder(task: Task) {
        val calendar = Calendar.getInstance()
        val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault())
        calendar.time = sdf.parse(task.deadline)!!

        // Check if the deadline is in the past (bug introduced here)
        if (calendar.timeInMillis < System.currentTimeMillis()) {
            val notificationId = tasks.indexOf(task)
            val builder = NotificationCompat.Builder(this, "task_reminder")
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle("Task Reminder")
                .setContentText("Don't forget to complete ${task.title}")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)

            createNotificationChannel()

            with(NotificationManagerCompat.from(this)) {
                notify(notificationId, builder.build())
            }
            }
        }

        private fun createNotificationChannel() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val name = "Task Reminder"
                val descriptionText = "Reminder for pending tasks"
                val importance = NotificationManager.IMPORTANCE_DEFAULT
                val channel = NotificationChannel("task_reminder", name, importance).apply {
                    description = descriptionText
                }
                val notificationManager: NotificationManager =
                    getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                notificationManager.createNotificationChannel(channel)
            }
        }

        private fun getDate(daysToAdd: Int): String {
            val calendar = Calendar.getInstance()
            calendar.add(Calendar.DAY_OF_YEAR, daysToAdd)
            val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault())
            return sdf.format(calendar.time)
        }
    }

data class Task(val title: String, val description: String, val deadline: String)

createNotificationChannel is not getting triggered

New contributor

Bob Matthew is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật