Notification addition on TODO app

i want to discuss a method to add notification when the reminder time is reached

This is an interesting idea. I will give you a complete idea of how you will implement this on your own.

to get a notification at a particular time you need a function to schedule a notification to be displayed by passing the time and notification as a parameter. where the notification will display the title of the todo item or anything else. This function will be called once you create a to-do item.

This scheduleNotification function uses AlarmManager to set the time to display the notification. along with all these you need to create a BroadcastReceiver which uses NotificationManager to create the notification.

For implementation reference, you can look at
https://developer.android.com/training/scheduling/alarms
which gives a better understanding of what you need to do.

Let me know if this helps

Hi, thank you for the info but I’m a beginner and am sorry to burden you , but can you explain this to me on call? i’m. a little slow

It’s not about being a beginner, you’ll eventually learn these in the course or when you gain some experience. these might seem a bit hard when you just started learning android.

All I can do for now is to provide the references and examples.
I highly suggest you to read the doc mentioned above

i have already seen the documentation and have a fair idea of what I need to do. I just need to discuss the implementation which will be difficult to do on chat. If possible can we arrange a call?

@gunin_2001 I’m sorry but this might take a while to figure it out through call. and I’m limited to chat or discuss. But I’m happy to help you with implementing this.

The following steps are required to implement this

  1. Implement a BroadcastReceiver example
class NotificationPublisher : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        //code to send a notification.
    }
}
  1. Add a receiver to app’s manifest (inside the application tag)
<receiver android :name= ".MyNotificationPublisher" />
  1. once you set the date and time in the TaskActivity you can get the scheduled time using
scheduledTime = myCalendar.time
  1. now create a function scheduleNotification which takes a notification and a long as parameter.
    fun scheduleNotification(notification: Notification, delay: long) {

        val notificationIntent: Intent = Intent(this, NotificationPublisher.class);
        notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
        notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
        val pendingIntent: PendingIntent  = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        val alarmManager: AlarmManager = context.getSystemService(Context.ALARM_SERVICE) as? AlarmManager
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
    }
  1. Create another to function to get a notification for to pass to scheduleNotification which is getNotification
    fun getNotification(String content): Notification {
        val builder: Notification.Builder = new Notification.Builder(this);
        builder.setContentTitle("ToDo Notification");
        builder.setContentText(content);
        builder.setSmallIcon(R.drawable.ic_launcher);
        return builder.build();
    }
  1. call the scheduleNotification function in the saveToDo function as follows
scheduleNotification(getNotification(), scheduledTime /* myCalender.time */)

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

I have tried implementing it but I was stuck, on where to make the Notification channel, also I had error with getSystemService() which I wasn’t able to resolve

Broadcast Receiver to send a notification.

class AlarmReceiver: BroadcastReceiver() {
    const val NOTIFICATION_ID = "notification-id"
    const val NOTIFICATION = "notification"
    override fun onReceive(context: Context, intent: Intent) {
        val notificationManager = ContextCompat.getSystemService(
            context,
            NotificationManager::class.java
        ) as NotificationManager
        val notification = intent.getParcelableExtra(NOTIFICATION)
        val id = intent.getExtra(NOTIFICATION_ID, 0)
        notificationManager.notify(id, notification)
    }
}

I am still getting 2 errors one is unresolved reference for NOTIFICATION_ID and NOTIFICATION the other is this line

val alarmManager: AlarmManager = context.getSystemService(Context.ALARM_SERVICE) as? AlarmManager

can you share the logs for the errors.


These are the 2 error lists and the code as well

use const val for Notification and id

and for alarm manager use

val alarmManager =
    context.getSystemService(Context.ALARM_SERVICE) as? AlarmManager

The above code I’ve mentioned is supposed to be a psuedo code I didn’t code it in the IDE, so that you can understand and implement by yourself

the alarm manager code you have mentioned is giving an error, it is showing that context does not have getSystemService and I used const val but that was showing an error as well that’s why I removed it. It is showing that ContextCompat has getSystemService function

Can you share the error for const val


Also can you please check the previous ss for task activity. IDE is not taking notification and notification_id as members

Add them above the NotificationPublisher class.
I’ll look into other errors later.

thank you for your time