Search code examples
iosuilocalnotificationibeacon

Beacon only one local notification a day


In my case, I wish the notification to appear only once per day.

This is my code:

- (void)viewDidLoad
{
[super viewDidLoad];

/*
 * BeaconManager setup.
 */
self.beaconManager = [[ESTBeaconManager alloc] init];
self.beaconManager.delegate = self;

NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"xxxalotofnumbersxxx"];

self.beaconRegion = [[ESTBeaconRegion alloc] initWithProximityUUID: uuid
                                                             major: 41270
                                                             minor: 64913
                                                        identifier: @"RegionIdentifier"];


[self.beaconManager startMonitoringForRegion:self.beaconRegion];

}

- (void)beaconManager:(ESTBeaconManager *)manager didEnterRegion:(ESTBeaconRegion *)region
{

UILocalNotification *notification = [UILocalNotification new];
notification.alertBody = @"Test";
notification.applicationIconBadgeNumber = 1;

[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

I can try to manage it well by inserting immediately after the start?

if(notification.applicationIconBadgeNumber = 1;)
   [self.beaconManager stopMonitoringForRegion:self.beaconRegion];

is there any other better solution to handle it? Thank you


Solution

  • Check how many local notification have been scheduled with [[UIApplication sharedApplication] scheduledLocalNotifications]; which returns a array of UILocalNotifcations. Then loop through all the UILocalNotification and check to see when it will be shown with its fireDate or userInfo.

    Use repeatInterval property in UILocalNotifcation to keep track of your daily notifications raither then always creating a new notification.

    if (![self isScheduledToday:[UIApplication sharedApplication].scheduledLocalNotifications]) {
        // add new notifcation as its not scheduled and set repeatMode to NSDayCalendarUnit;
    
    }    
    
    
    /**
     * returns if there a schedule for today
     */
    - (BOOL)isScheduledToday:(NSArray *)notifications {
        for (UILocalNotification *notification in notifications) {
            if ([self isSameDay:notification.fireDate]) {
                NSLog(@"Notifcation for today: %@", notification);
    
                return YES;
            }
    
        }
    
        return NO;
    }
    
    /**
     * checks if date matches today by comparing year, month and day
     */
    - (BOOL)isSameDay:(NSDate *)anotherDate {
        NSCalendar *calendar = [NSCalendar currentCalendar];
        calendar.timeZone = [NSTimeZone defaultTimeZone];
        NSDateComponents *components1 = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]];
        NSDateComponents *components2 = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:anotherDate];
    
        return (components1.year == components2.year &&
                components1.month == components2.month &&
                components1.day == components2.day);
    }