/** Schedules an alarm to go off a few minutes before this talk. */ private void scheduleNotification(Talk talk) { // We need to know the time slot of the talk, so fetch its data if we haven't already. if (!talk.isDataAvailable()) { Talk.getInBackground( talk.getObjectId(), new GetCallback<Talk>() { @Override public void done(Talk talk, ParseException e) { if (talk != null) { scheduleNotification(talk); } } }); return; } // Figure out what time we need to set the alarm for. Date talkStart = null != talk.getSlot() ? talk.getSlot().getStartTime() : null; if (BuildConfig.DEBUG) { Logger.getLogger(getClass().getName()).log(Level.INFO, "Registering alarm for " + talkStart); } long fiveMinutesBefore = null != talkStart ? talkStart.getTime() - (5000 * 60) : Long.MAX_VALUE; if (fiveMinutesBefore < System.currentTimeMillis()) { return; } // Register the actual alarm. AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); PendingIntent pendingIntent = getPendingIntent(talk); manager.set(AlarmManager.RTC_WAKEUP, fiveMinutesBefore, pendingIntent); }
/** Creates a PendingIntent to be sent when the alarm for this talk goes off. */ private PendingIntent getPendingIntent(Talk talk) { Intent intent = new Intent(); intent.setClass(context, FavoritesNotificationReceiver.class); intent.setData(talk.getUri()); return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT); }