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.