【问题标题】:Beacon only one local notification a dayBeacon 每天只有一个本地通知
【发布时间】:2014-07-17 11:37:53
【问题描述】:

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

这是我的代码:

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

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

【问题讨论】:

    标签: ios uilocalnotification ibeacon


    【解决方案1】:

    使用[[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);
    }
    

    【讨论】:

    • @mamo10 看到我对基本实现的更新,应该可以开始了...
    • 我们也可以在后台执行此操作吗?我想在一天内为同一个 ibeacon 显示 3 个通知,它也可以在后台工作。请帮忙
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多