Skip to content

Instantly share code, notes, and snippets.

@caseycoding
Last active April 1, 2016 16:59
Show Gist options
  • Save caseycoding/31afa9bad23eea12ad53 to your computer and use it in GitHub Desktop.
Save caseycoding/31afa9bad23eea12ad53 to your computer and use it in GitHub Desktop.
Background Local Notifications by Proximity with Secure Mode Beacons
//
// AppDelegate.m
// BCLocalNotifications
//
// Created by Damien Clarke on 24/05/2015.
// Copyright (c) 2015 Bluecats. All rights reserved.
//
//Note: you must select 'Uses Bluetooth LE Accessories' and request always location permission
//'Uses Bluetooth LE Accessories': https://github.com/bluecats/bluecats-ios-sdk/wiki/Ranging-Secure-Beacons-in-the-Background-and-Core-Data-Notification-Conflicts#ranging-secure-beacons-in-the-background
//Location use: https://github.com/bluecats/bluecats-ios-sdk/wiki/Location-Usage-Description-and-Requesting-Location-Access#location-usage-description
#import "AppDelegate.h"
#import "BlueCatsSDK.h"
#import "BCLocalNotificationManager.h"
#import "BCLocalNotification.h"
#import "BCCategory.h"
#import "BCMicroLocationManager.h"
#import "BCEventManager.h"
#import "BCBeacon.h"
@interface AppDelegate ()
@property (nonatomic) NSDateFormatter *dateFormatter;
@end
@implementation AppDelegate
- (NSDateFormatter *)dateFormatter
{
if (!_dateFormatter) {
_dateFormatter = [[NSDateFormatter alloc] init];
_dateFormatter.timeStyle = NSDateFormatterMediumStyle;
_dateFormatter.dateStyle = NSDateFormatterMediumStyle;
}
return _dateFormatter;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[BlueCatsSDK startPurringWithAppToken:@<#YOUR-APP-TOKEN#> completion:^(BCStatus status) {
if (status == kBCStatusPurringWithErrors) {
BCAppTokenVerificationStatus appTokenVerificationStatus = [BlueCatsSDK appTokenVerificationStatus];
if (appTokenVerificationStatus == kBCAppTokenVerificationStatusNotProvided || appTokenVerificationStatus == kBCAppTokenVerificationStatusInvalid) {
//kBCAppTokenVerificationStatusNotProvided - Use setAppToken to set the app token. Get an app token from app.bluecats.com
//kBCAppTokenVerificationStatusInvalid - App token invalid.
}
if (![BlueCatsSDK isLocationAuthorized]) {
[BlueCatsSDK requestAlwaysLocationAuthorization];
//[BlueCatsSDK requestWhenInUseLocationAuthorization]; "WhenInUse" only allows beacon ranging when the app is used.
}
if (![BlueCatsSDK isNetworkReachable]) {
//If this is the only error purring will be occur with network connectivity.
}
if (![BlueCatsSDK isBluetoothEnabled]) {
//Prompt user to enable bluetooth in settings. If BLE is required for current functionality a modal is recommended.
}
}
}];
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
}
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) {
//App has been automatically restarted by iOS while in the background
[self scheduleEventNotification];
}
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
[self scheduleEventNotification];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
-(void)scheduleEventNotification
{
BCEventManager *eventManager = [BCEventManager sharedManager];
[eventManager removeAllMonitoredEvents];
//Trigger first notification when in immediate proximity
BCTrigger* immediateTrigger = [[BCTrigger alloc] initWithIdentifier:@"Background - Immediate" andFilters:@[[BCEventFilter filterByCategoriesNamed:@[@"Entrance1234"]], [BCEventFilter filterByProximity:BCProximityImmediate]]];
immediateTrigger.validFrom = [NSDate dateWithTimeIntervalSinceNow:2.0f];
[eventManager monitorEventWithTrigger:immediateTrigger eventHandler:^(BCTriggeredEvent *triggeredEvent) {
NSLog(@"Triggered Immediate");
UILocalNotification* uiLocalNotification = [[UILocalNotification alloc] init];
uiLocalNotification.alertBody = [NSString stringWithFormat:@"Immediate: %@", [self.dateFormatter stringFromDate:[NSDate date]]];
uiLocalNotification.alertAction = @"View";
uiLocalNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:uiLocalNotification];
}];
//Trigger second notification when in far proximity
BCTrigger* farTrigger = [[BCTrigger alloc] initWithIdentifier:@"Background - Far" andFilters:@[[BCEventFilter filterByCategoriesNamed:@[@"Entrance1234"]], [BCEventFilter filterByProximity:BCProximityFar]]];
farTrigger.validFrom = [NSDate dateWithTimeIntervalSinceNow:2.0f];
[eventManager monitorEventWithTrigger:farTrigger eventHandler:^(BCTriggeredEvent *triggeredEvent) {
NSLog(@"Triggered Far");
UILocalNotification* uiLocalNotification = [[UILocalNotification alloc] init];
uiLocalNotification.alertBody = [NSString stringWithFormat:@"Far: %@", [self.dateFormatter stringFromDate:[NSDate date]]];
uiLocalNotification.alertAction = @"View";
uiLocalNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:uiLocalNotification];
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment