Search code examples
androidpush-notificationalarmmanagerandroid-alarms

Alarm manager make notification also after the date


My alarm manager works fine, but everytime I open my app AFTER the ALARM TIME it fires a notification. How can I say the alarm manager that It only do 1 notification and after the ALARM TIME it should stop.

public void startTimer(int year,int month, int day,int hour,int minute) {

             int time_year;
             int time_month;
             int time_day;
             int time_hour;
             int time_minute;

            time_minute = minute;
            time_hour = hour;
            time_month = month-1; // BC 0 januar and 11 dezember
            time_day = day;
            time_year = year;

            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, time_year);
            calendar.set(Calendar.MONTH, time_month);
            calendar.set(Calendar.DAY_OF_MONTH, time_day);
            calendar.set(Calendar.HOUR_OF_DAY, time_hour);
            calendar.set(Calendar.MINUTE, time_minute);
            calendar.set(Calendar.SECOND, 1);

            Intent intent = new Intent(getActivity(), AlarmReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity().getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(ALARM_SERVICE);
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);


        }

ALARM RECEIVER CLASS

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.w("myApp", "ALARM RECEIVER RECEIVED");

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent1 = new Intent(context,MainActivity.class);
        intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        //if we want ring on notifcation then uncomment below line//
//        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        PendingIntent pendingIntent = PendingIntent.getActivity(context,100,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context).
                setSmallIcon(R.drawable.online_icon).
                setContentIntent(pendingIntent).
                setContentText("Ein Event startet bald").
                setContentTitle("Hast du heute schon was vor?").
//                setSound(alarmSound).
        setAutoCancel(true);
        notificationManager.notify(100,builder.build());

    }
}

Solution

  • According to the docs this problem can occur at any time between the exact time and the next iteration. So if you set 2PM everyday, the alarm may go off between 2PM and the next day at 2PM.

    So add additional check if the time is before or after the alarm

    long timeToAlarm = calendar.getTimeInMillis();
    if (calendar.getTimeInMillis() < System.currentTimeMillis()){
        timeToAlarm += (24*60*60*1000);
    };
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);