繁体   English   中英

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

[英]Beacon only one local notification a day

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

这是我的代码:

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

还有其他更好的解决方案吗? 谢谢

使用[[UIApplication sharedApplication] scheduledLocalNotifications];检查已计划了多少个本地通知[[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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM