Skip to content

Commit

Permalink
Remind the user to launch the app if updates have stopped
Browse files Browse the repository at this point in the history
closes #154
  • Loading branch information
aaronpk committed May 9, 2024
1 parent 4747a52 commit 3a12c8d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions GPSLogger/GLManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static NSString *const GLTripModeStatsDefaultsName = @"GLTripModeStats";
static NSString *const GLVisitTrackingEnabledDefaultsName = @"GLVisitTrackingEnabledDefaults";

static NSString *const GLPurgeQueueOnNextLaunchDefaultsName = @"GLPurgeQueueOnNextLaunch";
static NSString *const GLLastScheduledNotificationDateDefaultsName = @"GLLastScheduledNotificationDateDefaults";

/* During-Trip Defaults */
static NSString *const GLTripDesiredAccuracyDefaultsName = @"GLTripDesiredAccuracyDefaults";
Expand Down
59 changes: 59 additions & 0 deletions GPSLogger/GLManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ @interface GLManager()
@property (strong, nonatomic) LOLDatabase *db;
@property (strong, nonatomic) FMDatabase *tripdb;

@property (strong, nonatomic) NSDate *lastScheduledNotificationDate;

@end

@implementation GLManager
Expand Down Expand Up @@ -722,6 +724,8 @@ - (void)enableTracking {
if(self.locationManager.location) {
self.lastLocation = self.locationManager.location;
}

[self scheduleLocalNotification];
}

- (void)disableTracking {
Expand All @@ -735,6 +739,7 @@ - (void)disableTracking {
[self.motionActivityManager stopActivityUpdates];
self.lastMotion = nil;
}
[self cancelLocalNotification];
}

- (void)sendingStarted {
Expand Down Expand Up @@ -778,6 +783,58 @@ - (void)sendQueueIfNotInProgress {
self.lastSentDate = NSDate.date;
}

#pragma mark - Scheduled local notifications

- (void)scheduleLocalNotification {
// Schedule a local notification for 10 minutes into the future to remind the user to launch the app.
// We'll cancel the notification when we get an update from the system, so this should only
// run if the app is shut down for some reason.

int scheduleRateLimit = 60;
int reminderIntervalSeconds = 600;

// Only do this at most once a minute so we don't hammer the system with scheduled notification requests
NSDate *lastScheduled = self.lastScheduledNotificationDate;
if(lastScheduled != nil && [lastScheduled timeIntervalSinceNow] > -1 * scheduleRateLimit) {
return;
}

[self cancelLocalNotification];

UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"Overland" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"Location updates were stopped. Launch the app to resume."
arguments:nil];
content.sound = [UNNotificationSound defaultSound];

UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:reminderIntervalSeconds repeats:NO];
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"reminder"
content:content trigger:trigger];

UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
self.lastScheduledNotificationDate = NSDate.now;
}];
}

- (void)cancelLocalNotification {
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center removePendingNotificationRequestsWithIdentifiers:@[@"reminder"]];
}

- (NSDate *)lastScheduledNotificationDate {
if([self defaultsKeyExists:GLLastScheduledNotificationDateDefaultsName]) {
return (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:GLLastScheduledNotificationDateDefaultsName];
} else {
return nil;
}
}
- (void)setLastScheduledNotificationDate:(NSDate *)date {
[[NSUserDefaults standardUserDefaults] setObject:date forKey:GLLastScheduledNotificationDateDefaultsName];
}


#pragma mark - Trips

+ (NSArray *)GLTripModes {
Expand Down Expand Up @@ -1668,6 +1725,8 @@ - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray
}

[self sendQueueIfTimeElapsed];

[self scheduleLocalNotification];
}

- (void)addMetadataToUpdate:(NSDictionary *) update {
Expand Down

0 comments on commit 3a12c8d

Please sign in to comment.