Android Notification triggering on application startup - java

I'm trying to get my Daily Notification Service working properly but I am stuck with an issue.
I should receive a notification only at a specific time of the day in the future but I noticed that
if (time_that_i_set_for_notification < current_time_of_the_day) notification triggers at the boot of my app
That's wrong because in that condition, the notification should trigger only the next day, not in the instant I launch the app.
Here's my attempt to get the things working:
I call this in my MainActivity, onCreate() method:
private void scheduleNotification(int hour, int minute){
Intent notificationIntent = new Intent(this, NotificationReceiver.class);
notificationIntent.putExtra("notifId", notifId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notifId, notificationIntent, 0);
// Set the alarm to start at approximately at a time
DatePicker datePicker = new DatePicker(this);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
if(calendar.getTime().compareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 12);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
}
I thought that adding if(calendar.getTime().compareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1); I would've achieved the result I needed.
Thank you and have a nice day.

Before calling setInexactRepeating, add this check:
if (calendar.getTimeInMillis() < System.currentTimeMillis())
calendar.setTimeInMillis(calendar.getTimeInMillis() + AlarmManager.INTERVAL_DAY);
Hope this helps.

Ok there's a wrong check there, if you change it, you should be able to solve your issue.
Here we go:
Intent notificationIntent = new Intent(this, NotificationReceiver.class);
notificationIntent.putExtra("notifId", notifId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notifId, notificationIntent, 0);
// Set the alarm to start at approximately at a time
DatePicker datePicker = new DatePicker(this);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
//if(calendar.getTime().compareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 50);
if (calendar.getTimeInMillis() < System.currentTimeMillis()) calendar.setTimeInMillis(calendar.getTimeInMillis() + AlarmManager.INTERVAL_DAY);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
I commented //if(calendar.getTime().compareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1);
because it should be unnecessary.
Let me know if it worked.
UPDATE: You should totally check whether the Service that handles your daily notification is high consume or not. A High-draining battery app would bother your users.

Related

Android Studio daily notification API 31+

I am attempting to set daily notifications in my app, but they only seem to be working one time. Additionally, I am attempting to make it work also on API 31 and higher.
int hourOfDay = timePicker.getHour();
int minute = timePicker.getMinute();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
if(calendar.getTimeInMillis() < System.currentTimeMillis())
calendar.add(Calendar.DATE, 1);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(TimerPickerAct.this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(TimerPickerAct.this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
I tried to do it this way but in this way I don't get any notification.
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent)

AlarmManager going off whenever app opens

I am trying to set an AlarmManager to go off for a specific time during the day. Here is my current code on it -
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 16);
calendar.set(Calendar.SECOND, 0);
long time = calendar.getTimeInMillis();
Intent intent = new Intent(MainActivity.this, Drawing.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
MainActivity.this, 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, time,
AlarmManager.INTERVAL_DAY, pendingIntent);
The drawing class displays a notification. The alarm does go off at the right time, but anytime the app is re-opened the notification goes off immediately.
I know its nothing to do with the drawing class because when i set the alarm to just go off with System.currentTimeInMillis() + 10 secs it works fine, even when app is reopening.
Any ideas? What is wrong with my logic?
You are not checking to see if calendar is in the past, as it will be ~2/3rds of the time (i.e., any time this code is run after 08:16). You will need to add() a day in that case, to get 8:16 for tomorrow.

Issues scheduling alarms when getting the time to set the AlarmManger for using a TimePicker because of AM/PM

I'm programming an app that has a feature that the user can use to set an alarm for an SMS message to be send to someone. The problem I am having is say the user sets the Alarm-Manager to go off at 9:00 A.M. and it is currently 10:00 P.M. then the alarm immediately goes off. It doesn't recognize the fact that the alarm should be set for the next day.
Here is the code for setting up the AlarmManager:
Intent intentAlarm = new Intent(MainMenu.this, AlarmReceiver.class);
intentAlarm.putExtra("phoneNumber", phoneNumber);
intentAlarm.putExtra("message", message);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), PendingIntent.getBroadcast(MainMenu.this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
Other than the AM/PM issue, the AlarmManager works well. I just don't know where to go from here and all of the documentation I am looking at just doesn't help.
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.HOUR_OF_DAY, 10); // use hour of day for am pm issue
cal1.set(Calendar.MINUTE, 00);
cal1.set(Calendar.SECOND, 00);
Calendar now = Calendar.getInstance();
if (now.after(cal1))
cal1.add(Calendar.DAY_OF_YEAR, 1);
// this code snippet add one day if the current time passed away
//else use cal1.set(year, month, day); for exact day
public void SetAlarm(Context context) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
// set alarm after 12 hours
long after = System.currentTimeMillis() + 12 * 60 * 60 * 1000;
am.set(AlarmManager.RTC_WAKEUP, after, pi);
}

Android - Trying to set an alarm on the first Monday of each month

I am trying to create an alarm on the first Monday of each month. Here is my code:
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, FilterOrderBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.WEEK_OF_MONTH, 1);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
if(Calendar.getInstance().getTimeInMillis() - calendar.getTimeInMillis() > 0 ) {
calendar.add(Calendar.MONTH, 1);
}
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() - calendar.getTimeInMillis(), pi);
Today is the 12th of June. This code creates an alarm trigger that is 1382399999 millis away or 15.9 days. That will only take me to the end of the 27th. I have thought about doing it with Joda, but there are issues with the Joda library with Android. It is really slow. Can anyone show me how to do this with the native Android libraries? Thanks for the help.

Android alarm manager

Hi i've been researching the alarm manager in android and was wondering if there how to set a specific time for an alarm (or could use notification manager)to go off at a specific time for example 12pm tomorrow .
The code below sets the alarm for 5 seconds from now so to set it for something like 12 pm could you do something like 12:00:00 or something?
Intent intent = new Intent(this, OnetimeAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), sender);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
Java's Date and Time libraries are a huge hassle, but this should give you an idea:
// the time now
Calendar calendar = Calendar.newInstance();
// noon tomorrow
calendar.add(Calendar.DAY_OF_YEAR, 1);
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
Calendar calendar = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
this should work

Categories