public static void sendNotifications() { //Remove UNUserNotificationCenter.Current.RemoveAllPendingNotificationRequests(); MyEventEntries myEvents = new MyEventEntries(); myEvents.loadJson(loadMyDatabase()); DateTime currentTime = DateTime.Now; foreach (EventEntry tempEvent in myEvents.Events) { // Rebuild notification var content = new UNMutableNotificationContent(); content.Title = "Event: " + tempEvent.Title + " starting soon"; content.Body = "Starts at " + tempEvent.StartTime.ToString("h:mm tt") + "\nLocation: " + tempEvent.Location; content.Badge = 1; // New trigger time // var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(5, false); var trigger = UNCalendarNotificationTrigger.CreateTrigger(DateTimeToNSDate(tempEvent.StartTime), false); // ID of Notification to be updated var requestID = tempEvent.EventID; var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger); // Add to system to modify existing Notification UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => { if (err != null) { // Do something with error... } }); } }
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) { Log.Debug(TAG, $"OnStartCommand called at {startTime}, flags={flags}, startid={startId}"); if (isStarted) { myEvents.loadJson(saveLoad.loadMyDatabase()); TimeSpan runtime = DateTime.UtcNow.Subtract(startTime); Log.Debug(TAG, $"This service was already started, it's been running for {runtime:c}."); } else { startTime = DateTime.UtcNow; Log.Debug(TAG, $"Starting the service, at {startTime}."); timer = new Timer(HandleTimerCallback, startTime, 0, TimerWait); isStarted = true; } return(StartCommandResult.Sticky); }
private void checkEvents() { MyEventEntries myEvents = new MyEventEntries(); PushNotificationsAndroid push = new PushNotificationsAndroid(); myEvents.loadJson(loadMyDatabase()); DateTime currentTime = DateTime.Now; List <EventEntry> temp = new List <EventEntry>(); foreach (EventEntry tempEvent in myEvents.Events) { int timeInterval = 15; int day = tempEvent.StartTime.Day; int month = tempEvent.StartTime.Month; int year = tempEvent.StartTime.Year; if (day == currentTime.Day && month == currentTime.Month && year == currentTime.Year) { int hour = tempEvent.StartTime.Hour; int minute = tempEvent.StartTime.Minute + hour * 60; int minuteNow = currentTime.Minute + currentTime.Hour * 60; if (minute - minuteNow < timeInterval && minute - minuteNow > 0) { temp.Add(tempEvent); } } } if (temp.Count > 1) { string tempString = string.Empty; foreach (EventEntry tempEvent in temp) { tempString += tempEvent.Title + "\n"; } push.SendPush(temp.Count + " Events coming up in the next 15 minutes", tempString, this); } else if (temp.Count == 1) { push.SendPush("Event: " + temp[0].Title + " starting soon", "Starts at " + temp[0].StartTime.ToString("h:mm tt") + "\nLocation: " + temp[0].Location, this); } }