Reputation: 3134
I'm sending a Local Notification
every day at the same time.
I was wondering if there were a way to not send the notification if the user had already opened the app that day?
Here's the code I'm using to check if notifications are allowed, and then send the notification:
- (void)viewDidLoad {
[super viewDidLoad];
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
// before we create any new ones, cancel all existing notifications
[[UIApplication sharedApplication]cancelAllLocalNotifications];
if ([[[UIApplication sharedApplication] currentUserNotificationSettings] types] != UIUserNotificationTypeNone) {
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comp = [cal components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:[NSDate date]];
comp.hour = 19; // 19 = 7PM
comp.minute = 45; // 7:45 PM
comp.second = 01; // 7:45:01 PM
localNotification.fireDate = [cal dateFromComponents:comp];
localNotification.alertBody = @"Local Notification in iOS8";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSCalendarUnitDay;
// this will schedule the notification to fire at the fire date
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// this will fire the notification right away, it will still also fire at the date we set
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
}
Edit: Here's the code I'm trying to implement the method suggestion per the answer below with? I'm obviously not getting something right here though :/
- (void)isDateLowerThanToday {
NSDate *now = [NSDate date];
int daysToAdd = -1;
NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*daysToAdd];
NSLog(@"NewDate1 = %@", newDate1);
}
Upvotes: 1
Views: 224
Reputation: 68400
You can store on NSUserDefaults
the last notification sent date. Then, when app opens you check this value, if it's null or lower than Today you send the notification and updates the value on NSUserDefaults
with current date.
Something like this should do the trick
NSString *key = @"LAST_NOTIFICATION_SENT_DATE";
NSDate *date = [[NSUserDefaults standardUserDefaults] objectForKey:key];
// You have to implement isDateLowerThanToday
if (!date || [self isDateLowerThanToday:date]) {
[self sendNotification];
date = [NSDate date];
[[NUserDefaults standardUserDefaults] setObject:date forKey:key];
}
Here you have isDateLowerThanToday
implementation
-(bool)isDateLowerThanToday:(NSDate *)date {
NSDate * now = [NSDate date];
NSComparisonResult result = [now compare:date];
return result == NSOrderedDescending;
}
Upvotes: 3