Skip to content

Instantly share code, notes, and snippets.

@caseycoding
Last active November 5, 2020 14:09
Show Gist options
  • Save caseycoding/722ffe3f85cd6ce5c392 to your computer and use it in GitHub Desktop.
Save caseycoding/722ffe3f85cd6ce5c392 to your computer and use it in GitHub Desktop.
iBeacon Background Notification
//
// AppDelegate.m
// BCLocalNotifications
//
// Created by Damien Clarke on 24/05/2015.
// Copyright (c) 2015 Bluecats. All rights reserved.
//
#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-HERE#>" 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];
NSLog(@"Appliation launched in the background");
}
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.
}
- (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];
NSArray* proximities = @[[NSNumber numberWithInt:BCProximityImmediate],[NSNumber numberWithInt:BCProximityNear],[NSNumber numberWithInt:BCProximityFar]];
//Trigger notification when in proximity
BCTrigger* immediateTrigger = [[BCTrigger alloc] initWithIdentifier:@"Background" andFilters:@[[BCEventFilter filterByCategoriesNamed:@[@"Entrance1234"]], [BCEventFilter filterByProximities:proximities]]];
immediateTrigger.validFrom = [NSDate dateWithTimeIntervalSinceNow:2.0f];
[eventManager monitorEventWithTrigger:immediateTrigger eventHandler:^(BCTriggeredEvent *triggeredEvent) {
NSLog(@"Triggered Notification");
UILocalNotification* uiLocalNotification = [[UILocalNotification alloc] init];
uiLocalNotification.alertBody = [NSString stringWithFormat:@"Beacon found at: %@", [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