I am developing an Android application for counseling services. Client can view their scheduled appointment in app. For example,
Next appointment: Dec 31 2016 10:00AM
Now I need to do that user will receive 2 notifications – reminders about appointment. One on 7 days before and another on 3 days before. I save this date (Dec 31 2016 10:00AM) as a String
so I can extract year, month, etc.
I found that I need to write some kind of service which will send these notifications. This is what I tried (is not completed):
public class NotificationService extends Service {
@Override
public void onCreate() {
Intent resultIntent=new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);
Notification nBuilder = new Notification.Builder(this)
.setContentTitle("Don't miss! ")
.setTicker("Notification!")
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.my_logo)
.setContentText("7 days left till your appointment...")
//.setWhen(System.currentTimeMillis())
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nBuilder.flags |=Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1,nBuilder);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
And method that I don't know from where to call:
public void reminder() {
Intent intent = new Intent(getActivity(), MainActivity.class);
AlarmManager manager =(AlarmManager) getActivity().getSystemService(Activity.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(getActivity().getApplicationContext(),
0,intent, 0);
Calendar cal=Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 8);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
manager.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),24*60*60*1000,pendingIntent);
}
For testing purposes I have set hour/minute/second manually but obviously I will need to extract it from date String
.