I have a calendar library I grabbed off of github: https://github.com/prolificinteractive/material-calendarview
And I'm having the user click on a date and add a reminder for that date, an alert dialog then pops up and asks them to enter the time they would like to be reminded on that day.
Now I was able to convert the text into a simpledate format and I spit it out into a string from a calendar object, so the date and time should be passing through the notification. But it doesn't seem to work anyways
Heres the code that sets the alarm:
Calendar cal = Calendar.getInstance();
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getService(context, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
try {
cal.setTime(alertFormatter.parse(date));
System.out.print("Date added successfully");
} catch (ParseException e) {
System.out.println("Failed to add date");
}
cal.add(Calendar.HOUR, Integer.parseInt(hour.getText().toString()));
cal.add(Calendar.MINUTE, Integer.parseInt(minute.getText().toString()));
cal.add(Calendar.SECOND, 0);
if(spAMpm.getSelectedItem().equals("AM"))cal.add(Calendar.AM_PM, Calendar.AM);
else if (spAMpm.getSelectedItem().equals("PM"))cal.add(Calendar.AM_PM, Calendar.PM);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
Then the receiver I created to do what I need it to do:
public class UpcomingWorkNotification extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, UpcomingWork.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(UpcomingWork.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Four.oh")
.setContentText("Assignment Due Soon!")
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
}
In the manifest I gave it this permission and added the receiver
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<receiver android:name=".UpcomingWorkNotification">
<action android:name="android.media.action.DISPLAY_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</receiver>