Skip to content

Instantly share code, notes, and snippets.

@JoeyBodnar
Created June 26, 2019 16:29
Show Gist options
  • Save JoeyBodnar/aaf291271d86ff5aaff80282e9f858a1 to your computer and use it in GitHub Desktop.
Save JoeyBodnar/aaf291271d86ff5aaff80282e9f858a1 to your computer and use it in GitHub Desktop.
import UIKit
import NearBee
import CoreLocation
import CoreBluetooth
import UserNotifications
import CoreData
class ViewController: UIViewController {
let backgroundImageView = UIImageView()
let tableView = UITableView()
let options = []
let locationManager = CLLocationManager()
var currentLocation = CLLocation()
var nearBee: NearBee!
override func loadView() {
super.loadView()
layout()
}
override func viewDidLoad() {
super.viewDidLoad()
UserDefaults.standard.setValue(true, forKey: "NearBeeToggleSound")
let localCatergory = UNNotificationCategory(identifier: "nearbyNotificationView", actions: [], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([localCatergory])
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
}
UNUserNotificationCenter.current().delegate = self
nearBee = NearBee.initNearBee()
nearBee?.delegate = self
nearBee?.enableBackgroundNotification(true)
nearBee?.startScanning()
if let url = Bundle.main.url(forResource: "notification", withExtension: "mp3") {
nearBee?.notificationSound = UNNotificationSoundName(rawValue: url.lastPathComponent)
}
setupViews()
setupLocationManager()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
nearBee?.delegate = self
}
func setupLocationManager() {
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.desiredAccuracy = 100
locationManager.startUpdatingLocation()
}
}
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == CLAuthorizationStatus.authorizedAlways || status == .authorizedWhenInUse {
locationManager.startUpdatingLocation()
}
}
}
extension ViewController: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
guard let authToken = Helpers.currentAuthorizationToken() else { return }
let isNearBeeNotification = nearBee.checkAndProcessNotification(response.notification, queryParameters: ["authToken" : authToken])
BeaconCacheManager.cacheBeacon(from: response)
if (isNearBeeNotification) {
completionHandler()
}
}
}
extension ViewController: NearBeeDelegate {
func didUpdateState(_ state: NearBeeState) {
}
func didLoseBeacons(_ beacons: [NearBeeBeacon]) {
}
func didUpdateBeacons(_ beacons: [NearBeeBeacon]) {
}
func didFindBeacons(_ beacons: [NearBeeBeacon]) {
}
func didThrowError(_ error: Error) {
}
}
extension ViewController {
fileprivate func setupViews() {
// ....setup views and add attributes her
}
fileprivate func layout() {
// ....layout here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment