每天只有一个本地通知信标

时间:2014-07-17 11:37:53

标签: ios uilocalnotification ibeacon

就我而言,我希望通知每天只出现一次。

这是我的代码:

- (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];
}

我可以尝试在开始后立即插入来管理它吗?

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

有没有其他更好的解决方案来处理它?谢谢

1 个答案:

答案 0 :(得分:0)

检查已调度了多少本地通知,其中[[UIApplication sharedApplication] scheduledLocalNotifications];返回了一组UILocalNotifcations。然后循环遍历所有UILocalNotification并检查它将在何时以其fireDate或userInfo显示。

在UILocalNotifcation中使用repeatInterval属性来跟踪您的每日通知,然后始终创建新通知。

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);
}