Hi guys
I'm messing around with notifications based upon location. At the moment I only want to fire the notification when the app is in the background. For some strange reason the notification isn't triggering. The request is added however to the notification center. Does anyone know what I'm doing wrong? Or is it a bug in the simulator?
Thanks
AppDelegate.swift
ViewController.swift
I'm messing around with notifications based upon location. At the moment I only want to fire the notification when the app is in the background. For some strange reason the notification isn't triggering. The request is added however to the notification center. Does anyone know what I'm doing wrong? Or is it a bug in the simulator?
Thanks
AppDelegate.swift
Code:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
if granted {
print("permission was granted")
} else {
print("permission was not granted")
}
}
return true
}
ViewController.swift
Code:
import UIKit
import UserNotifications
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
let manager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.manager.delegate = self
if CLLocationManager.authorizationStatus() == .notDetermined || CLLocationManager.authorizationStatus() == .denied {
self.manager.requestWhenInUseAuthorization()
self.manager.requestAlwaysAuthorization()
print("asking")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - CLLocationManagerDelegate methods
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedAlways {
manager.startUpdatingLocation()
addRequestToUNUserNoticationCenter()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(manager.location?.coordinate.longitude)
// print(locations.last?.coordinate.latitude)
}
// MARK: - Homebaked methods
private func addRequestToUNUserNoticationCenter() {
// Creating the content
let content = UNMutableNotificationContent()
content.title = "Entering London"
content.subtitle = "The capital of Great Brittain"
content.body = "London is the capital and most populous city of England and the United Kingdom. \n\n Standing on the River Thames in the south east of the island of Great Britain, London has been a major settlement for two millennia. It was founded by the Romans, who named it Londinium. London's ancient core, the City of London, largely retains its 1.12-square-mile (2.9 km2) medieval boundaries. Since at least the 19th century, \"London\" has also referred to the metropolis around this core, historically split between Middlesex, Essex, Surrey, Kent, and Hertfordshire, which today largely makes up Greater London, a region governed by the Mayor of London and the London Assembly."
//Creating the location trigger
let center = CLLocationCoordinate2DMake(51.509865, -0.133092)
let region = CLCircularRegion(center: center, radius: 20000.0, identifier: "London city")
region.notifyOnEntry = true
region.notifyOnExit = false
let trigger = UNLocationNotificationTrigger(region: region, repeats: true)
// Adding the notification(request) to the notification center
let request = UNNotificationRequest(identifier: "UserNotificationsByLocation.HQ", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
if let error = error {
print("Error is thrown \(error)")
}
}
UNUserNotificationCenter.current().getPendingNotificationRequests { requests in
for request in requests {
print(request)
}
}
}
}