-4

I am developing an app in which i have to get a daily notification at a specific time,how can i code this?
let's say i have to get a notification at 8:00 am daily and when i click that notification it has to open my app.

CodeWithVikas
  • 1,105
  • 9
  • 33
Rabbani
  • 29
  • 10

2 Answers2

2

There is something called Alarm manager, you can read about that here: Alarm manager

or some kind of timer like in this example

Coder123
  • 784
  • 2
  • 8
  • 29
0

You can use this code to schedule a repeating alarm

AlarmManager alarmMgr;
PendingIntent alarmIntent;
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
// Set the alarm to start at 8:00 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);

Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// setRepeating() lets you specify a precise custom interval--in this case,
// one day.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);

this only set the alarm, when system fires this alarm you need to receive this in your broadcast receiver and make a notification from there i.e in this case AlarmReceiver.class

Hamza Khan
  • 84
  • 2
  • yes it does, but not that `setRepeating` will set inexact alarms from API LEVEL 19 as it is mentioned in docs ```Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.``` – Hamza Khan Nov 27 '19 at 05:22