Android Studio daily notification API 31+ - java

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)

Related

AlarmManager.setRepeating stops working after a while

Below is my code, it works great for the first 7 or so days but after that it just stops. I am not sure why?
I am having much the same problem as this person:
here
Any help will be appreciated:)
Intent _intent = new Intent(context, AlarmBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, _intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, sec);
// every 24 hours (daily) at the time set
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * minTime * MainSetTimeInt, pendingIntent);

Android Notification triggering on application startup

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.

Set repeating alarm

i want to set alarm to repeat it in a particular day .for example every monday at 4:20 ,
I have created a method but not working .please help me to solve this problem
public void setAlarm(int day,int hour,int minit){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE,minit );
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.DAY_OF_WEEK, day);
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Long alarmTime = cal.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 24 * 60 * 60 * 1000 *7, pendingIntent);
}
now if i choose monday 2:40 its only alarming on next monday 2:40 not working on every monday 2:40
You can try use setInexactRepeating instead.As of API 19, all repeating alarms are inexact.You can refer to the official documents in detail.
Use setInexactRepeating function it worked for me.

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