Why doesn’t a scheduled notification appear at the fixed time, and instead of that, the application goes back to the main activity in Android Java?

  Kiến thức lập trình

I want to push a notification in my Android application at a fixed time one day before a specific date. I have already created a channel in the main activity.

Here is the code of the method responsible for scheduling notifications:

private void scheduleNotification(LocalDate date, String doctorName) {
    try {
        if (date != null) {
            LocalDate notificationDate = date.minusDays(1);            Calendar calendar = Calendar.getInstance();            calendar.set(notificationDate.getYear(), notificationDate.getMonthValue() - 1, notificationDate.getDayOfMonth(), 2, 2, 0);            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);            Intent intent = new Intent(getBaseContext(), AppointmentNotificationReceiver.class);            intent.putExtra("doctor_name", doctorName);            PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent, PendingIntent.FLAG_IMMUTABLE);            alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);            Toast.makeText(getBaseContext(), "Notification scheduled for " + notificationDate.format(DateTimeFormatter.ofPattern("dd.MM.yyyy")) + " at 13:00.", Toast.LENGTH_LONG).show();        }
    } catch (Exception e) {
        Toast.makeText(getBaseContext(), "Error scheduling notification: " + e.getMessage() + ".", Toast.LENGTH_LONG).show();    }
}

And this is the code of the notification receiver class.

public class AppointmentNotificationReceiver extends BroadcastReceiver {
    @Override    public void onReceive(Context context, Intent intent) {
        String doctorName = intent.getStringExtra("doctor_name");        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "ID12345678FFRREE")
                .setContentTitle("Remainder")
                .setContentText("Tomorrow.")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);        if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling            //    ActivityCompat#requestPermissions            // here to request the missing permissions, and then overriding            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,            //                                          int[] grantResults)            // to handle the case where the user grants the permission. See the documentation            // for ActivityCompat#requestPermissions for more details.            return;        }
        notificationManager.notify(200, builder.build());    }
}

Everything is okay, but the notification does not appear at all. Instead, at the fixed time of the notification, the application goes back to the first activity (main activity) if I am currently at a different activity.

LEAVE A COMMENT