benGshore
benGshore

Reputation: 65

How To Limit The Amount Of Notifications

I have 3 separate ibeacons placed in 3 different rooms. When entering the region of the beacon the didRangeBeacon method runs every second, Thus resulting in an infinite number of notifications when in range.

This is the code i have:

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

            }
        }

    }

Can anybody help me so that it gives one notification upon entry, and that when i then walk to the next beacon i get another notification specific to that beacon. Thanks.

Upvotes: 1

Views: 116

Answers (1)

ntsh
ntsh

Reputation: 759

Use the locationManager:didEnterRegion: method.

This method is called when the user enters a beacon region defined by your app.

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

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

}

Upvotes: 1

Related Questions