0

I created an application that allows you to enter the content of the notification and select the date and time at which the notification should be displayed. Everything works fine, except that the text entered in EditText by the user does not appear in the displayed notification. How to solve it? Thank you in advance!

MainActivity

package com.example.kalendarz_2 import android.annotation.SuppressLint import android.app.AlarmManager import android.app.DatePickerDialog import android.app.PendingIntent import android.content.Context import android.content.Intent import android.os.Build import android.os.Bundle import android.view.View import android.widget.Button import android.widget.DatePicker import android.widget.EditText import android.widget.TimePicker import android.app.TimePickerDialog import android.widget.Toast import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity import java.util.* class MainActivity : AppCompatActivity() { private lateinit var alarmManager: AlarmManager override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Inicjalizacja alarmManager alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager } @RequiresApi(Build.VERSION_CODES.S) fun showDateTimePicker(view: View) { val calendar = Calendar.getInstance() val initialYear = calendar.get(Calendar.YEAR) val initialMonth = calendar.get(Calendar.MONTH) val initialDay = calendar.get(Calendar.DAY_OF_MONTH) val initialHour = calendar.get(Calendar.HOUR_OF_DAY) val initialMinute = calendar.get(Calendar.MINUTE) val datePickerDialog = DatePickerDialog(this, { _, year, month, day -> val timePickerDialog = TimePickerDialog(this, { _, hourOfDay, minute -> setReminder(year, month, day, hourOfDay, minute) }, initialHour, initialMinute, true) timePickerDialog.show() }, initialYear, initialMonth, initialDay) datePickerDialog.show() } @RequiresApi(Build.VERSION_CODES.S) private fun setReminder(year: Int, month: Int, day: Int, hourOfDay: Int, minute: Int) { val calendar = Calendar.getInstance() calendar.set(year, month, day, hourOfDay, minute) if (calendar.timeInMillis > System.currentTimeMillis()) { if (alarmManager.canScheduleExactAlarms()) { val reminderIntent = Intent(this, ReminderReceiver::class.java) reminderIntent.putExtra("REMINDER_TEXT", "Treść przypomnienia") reminderIntent.putExtra("REMINDER_TIME", calendar.timeInMillis) val pendingIntent = PendingIntent.getBroadcast( this, 0, reminderIntent, PendingIntent.FLAG_IMMUTABLE ) alarmManager.setExactAndAllowWhileIdle( AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent ) Toast.makeText( this, "Notification set at: ${calendar.time}", Toast.LENGTH_SHORT ).show() } else { // Handle the case where the device doesn't support exact alarms Toast.makeText(this, "Device doesn't support exact alarms", Toast.LENGTH_SHORT).show() } } else { Toast.makeText(this, "Enter date from the future", Toast.LENGTH_SHORT).show() } } } 

ReminderReceiver

package com.example.kalendarz_2 import android.app.Notification import android.app.NotificationChannel import android.app.NotificationManager import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.os.Build import android.util.Log import android.widget.Toast import androidx.core.app.NotificationCompat import com.example.kalendarz_2.R class ReminderReceiver : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { Log.d("ReminderReceiver", "Received reminder broadcast") val reminderText = intent?.getStringExtra("REMINDER_TEXT") ?: "No content notification" val reminderTime = intent?.getLongExtra("REMINDER_TIME", 0) ?: 0 Log.d("ReminderReceiver", "Reminder text: $reminderText") Log.d("ReminderReceiver", "Reminder time: $reminderTime") val currentTime = System.currentTimeMillis() if (reminderTime > currentTime) { showReminderNotification(context, reminderText) } else { Toast.makeText(context, "Notification: $reminderText", Toast.LENGTH_LONG).show() } } private fun showReminderNotification(context: Context?, reminderText: String) { val notificationManager = context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val notificationChannelId = "channel_id" val notificationBuilder = NotificationCompat.Builder(context, notificationChannelId) .setContentTitle("Notification") .setContentText("Notification: $reminderText") .setSmallIcon(R.mipmap.ic_launcher) .setAutoCancel(true) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val notificationChannel = NotificationChannel( notificationChannelId, "Notifications", NotificationManager.IMPORTANCE_HIGH ) notificationManager.createNotificationChannel(notificationChannel) notificationBuilder.setChannelId(notificationChannelId) } notificationManager.notify(1, notificationBuilder.build()) } } 

ReminderService

package com.example.kalendarz_2 import android.app.Notification import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent import android.app.Service import android.content.Context import android.content.Intent import android.os.Build import android.os.IBinder import android.util.Log import androidx.annotation.RequiresApi class ReminderService : Service() { override fun onBind(intent: Intent?): IBinder? { return null } @RequiresApi(Build.VERSION_CODES.O) override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { Log.d("ReminderService", "Reminder service started") showReminderNotification(intent?.getStringExtra("REMINDER_TEXT") ?: "no reminder content") return START_STICKY } @RequiresApi(Build.VERSION_CODES.O) private fun showReminderNotification(reminderText: String) { val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val channelId = "channel_id" val channelName = "Reminders" val importance = NotificationManager.IMPORTANCE_HIGH if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel(channelId, channelName, importance) notificationManager.createNotificationChannel(channel) } val notificationIntent = Intent(this, MainActivity::class.java) val pendingIntent = PendingIntent.getActivity( this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT ) val notificationBuilder = Notification.Builder(this, channelId) .setContentTitle("Reminder") .setContentText(reminderText) .setSmallIcon(android.R.drawable.ic_lock_idle_alarm) .setContentIntent(pendingIntent) .setAutoCancel(true) notificationManager.notify(1, notificationBuilder.build()) } } 
3
  • Maybe because you have hard-coded the reminder text in your setReminder method ? reminderIntent.putExtra("REMINDER_TEXT", "Treść przypomnienia") Commented Feb 1, 2024 at 20:37
  • Sorry, but I'm a beginner and I don't really know how to change it... Could you tell me how to change it? Commented Feb 1, 2024 at 20:54
  • I change to this, but it still doesn't work: if (calendar.timeInMillis > System.currentTimeMillis()) { val reminderText = findViewById<EditText>(R.id.etReminderText).text.toString() if (alarmManager.canScheduleExactAlarms()) { val reminderIntent = Intent(this, ReminderReceiver::class.java) reminderIntent.putExtra("REMINDER_TEXT", reminderText) reminderIntent.putExtra("REMINDER_TIME", calendar.timeInMillis) val pendingIntent = PendingIntent.getBroadcast( this, 0, reminderIntent, PendingIntent.FLAG_IMMUTABLE Commented Feb 1, 2024 at 21:01

1 Answer 1

1
  • First of all, you have to get the text from the EditText where the user enters the notification's text. Let's assume that you have an EditText with an ID reminderEditText. You would get the text like this :
val editTextR = findByView<EditText>(R.id.reminderEditText) val userText = reminderEditText.text.toString() 
  • Secondly, pass the input text to the Intent :
reminderIntent.putExtra("REMINDER_TEXT", userText) 

Method setReminder :

@RequiresApi(Build.VERSION_CODES.S) private fun setReminder(year: Int, month: Int, day: Int, hourOfDay: Int, minute: Int) { val calendar = Calendar.getInstance() calendar.set(year, month, day, hourOfDay, minute) if (calendar.timeInMillis > System.currentTimeMillis()) { if (alarmManager.canScheduleExactAlarms()) { val reminderIntent = Intent(this, ReminderReceiver::class.java) val editTextReminder = findViewById<EditText>(R.id.reminderEditText) val userText = editTextReminder.text.toString() reminderIntent.putExtra("REMINDER_TEXT", userText) reminderIntent.putExtra("REMINDER_TIME", calendar.timeInMillis) val pendingIntent = PendingIntent.getBroadcast( this, 0, reminderIntent, PendingIntent.FLAG_IMMUTABLE ) alarmManager.setExactAndAllowWhileIdle( AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent ) Toast.makeText( this, "Notification set at: ${calendar.time}", Toast.LENGTH_SHORT ).show() } else { Toast.makeText(this, "Device doesn't support exact alarms", Toast.LENGTH_SHORT).show() } } else { Toast.makeText(this, "Enter date from the future", Toast.LENGTH_SHORT).show() } } 

Remember to replace R.id.reminderEditText with the actual ID of the EditText in your layout.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much for your answer, but it still doesn't work... The notification is displayed with the original text still
Interestingly, the text in the notification that I entered first when using this application is displayed
When I first started the application, I entered the text "aaa". Now, every time I set a reminder and type some text, a notification with the text "aaa" appears.
Do you have any ideas? Where is mistake?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.