Set notification for specific time

163 views Asked by At

I have currently created a notification which displays no problem when the switch is checked, however I want to create an if statement to set a current time of when the notification will be displayed. I have tried the below however the notification doesn't display at the 3:30pm when tested, am I missing anything or how can I do this?

    public void onCheckedChanged(CompoundButton buttonView, boolean notificationSwitchisChecked) {
          if(notificationSwitchisChecked){
               Calendar.getInstance();
               if (Calendar.HOUR_OF_DAY == 15 && Calendar.MINUTE == 30 && Calendar.SECOND == 0){
                 sendNotification(); // this method displays the notification no problem when checked
                    }
               }
1

There are 1 answers

0
Apps Maven On

You can use an alarm manager

Intent myIntent = new Intent(ThisApp.this , myService.class);     
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(ThisApp.this, 0, myIntent, 0);

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent);  //set repeating every 24 hours

and put the notification inside myService class!