如何限制通知的数量

时间:2014-09-18 15:16:25

标签: objective-c uilocalnotification ibeacon

我有3个独立的ibeacons放在3个不同的房间里。当进入信标区域时,didRangeBeacon方法每秒运行一次,因此在范围内时会产生无限数量的通知。

这是我的代码:

BOOL _isInsideRegion;

    - (void)locationManager:(CLLocationManager*)manager didRangeBeacons:(NSArray*)beacons inRegion:(CLBeaconRegion*)region {




        CLBeacon *firstBeacon = [beacons firstObject];


        int major = [firstBeacon.major intValue];
        int minor = [firstBeacon.minor intValue];




        if (major == 43005 && minor == 52679) {

            if (!_isInsideRegion) {

                UILocalNotification *notification = [[UILocalNotification alloc] init];
                notification.soundName = @"Default";
                notification.alertBody = @"Green";
                [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
                 self.beaconColour.text = @"Green";
                self.minor.text = [NSString stringWithFormat:@"%D", minor];
                self.major.text = [NSString stringWithFormat:@"%D", major];

            }
        }
        else if (major == 48891 && minor == 47852) {

            if (!_isInsideRegion) {

                UILocalNotification *notification = [[UILocalNotification alloc] init];
                notification.soundName = @"Default";
                notification.alertBody = @"blue";
                [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
                 self.beaconColour.text = @"Blue";
                self.minor.text = [NSString stringWithFormat:@"%D", minor];
                self.major.text = [NSString stringWithFormat:@"%D", major];

            }
        }
        else if (major == 59510 && minor == 42953) {

            if (!_isInsideRegion) {

                UILocalNotification *notification = [[UILocalNotification alloc] init];
                notification.soundName = @"Default";
                notification.alertBody = @"dark blue";
                [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
                 self.beaconColour.text = @"Dark Blue";
                self.minor.text = [NSString stringWithFormat:@"%D", minor];
                self.major.text = [NSString stringWithFormat:@"%D", major];

            }
        }

    }

任何人都可以帮助我,以便在进入时提供一个通知,然后当我走到下一个信标时,我会得到另一个特定于该信标的通知。 感谢。

1 个答案:

答案 0 :(得分:1)

使用locationManager:didEnterRegion:方法。

当用户输入您的应用定义的信标区域时,将调用此方法。

- (void)locationManager:(CLLocationManager *)manager
     didEnterRegion:(CLBeaconRegion *)region {

    NSLog(@"Did Enter Region for %@", region.identifier);
    //Show Notification

}