0

I've tried to follow this Stack Overflow post for setting a notification for future dates but something isn't working as the notification never comes. Let me know if more information is needed

class AddReminderActivity : AppCompatActivity() {

        val saveButton: Button = findViewById(R.id.reminder_save_button)
        saveButton.setOnClickListener {
            val editText: EditText = findViewById(R.id.reminder_title)
            val reminderTitle = editText.text.toString()

            val intent = Intent(this, Receiver::class.java)
            intent.putExtra("reminderTitle", reminderTitle)
            val pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_IMMUTABLE)

            val am: AlarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
            am.set(AlarmManager.RTC_WAKEUP, combinedCal.timeInMillis, pendingIntent)
       }
}



class Receiver : Service() {
    lateinit var reminderTitle: String
    override fun onBind(intent: Intent): IBinder? {
        return null
    }

    override fun onCreate() {
        val intent = Intent(this, AddReminderActivity::class.java)
        val pattern = longArrayOf(0, 300, 0)
        val pi = PendingIntent.getActivity(this, 1234, intent, PendingIntent.FLAG_IMMUTABLE)
        reminderTitle = intent.getStringExtra("reminderTitle").toString()

        val builder = NotificationCompat.Builder(this)
            .setContentTitle(reminderTitle)
            .setVibrate(pattern).setAutoCancel(true)

        builder.setContentIntent(pi)
        builder.setDefaults(Notification.DEFAULT_SOUND)
        builder.setAutoCancel(true)
        val notificationManager =
            this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.notify(1234, builder.build())

    }
}
Irene Serrano
  • 33
  • 1
  • 1
  • 10

1 Answers1

0

is your code working properly but you don't get notification then go to your app setting and give notification permission then run again and set a alarm then check. It will work.

if still get problem then check my this answer

How to schedule recurring notifications in kotlin?

Niaj Mahmud
  • 399
  • 3
  • 10