UNLocationNotificationTrigger- Not working in simu

2019-04-20 09:36发布

Using User Notification framework available in iOS 10 i tried to trigger notification whenever User enters specific geo location using UNLocationNotificationTrigger. When I tried test it through simulator by simulating the Geo location, the notification is not getting triggered, but Location manager returns the updated geo location. Should this be tested in real device instead of running it in simulator?

1条回答
霸刀☆藐视天下
2楼-- · 2019-04-20 10:10

According to Apple Documentation :

Apps must request access to location services and must have when-in-use permissions to use this class. To request permission to use location services, call the requestWhenInUseAuthorization() method of CLLocationManager before scheduling any location-based triggers.

However with my emulators/devices "when-in-use" permissions are not enough, permissions must be set to "Always".

Thus, add this key to your pinfo.list

<key>NSLocationAlwaysUsageDescription</key>
<string>We use your location to warn you when there are adorable cats nearby</string>

Then activate location. Define your trigger only once you're certain you're authorized always, for example I did it here in didChangeAuthorizationStatus:

class myClass : CLLocationManagerDelegate {

var locationManager: CLLocationManager() 

func init() {
   // Note: defining the location manager locally in this function won't work
   //    var locationManager: CLLocationManager() 
   // as it gets gargabe collected too early.

   locationManager.delegate = self
   locationManager.requestAlwaysAuthorization()
   UNUserNotificationCenter.current().delegate = self
   UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) {(accepted, error) in
   if !accepted {
         logger.info("Notification access denied.")
   }

}

// MARK CLLocationManagerDelegate:
func locationManager(manager: CLLocationManager,
                     didChangeAuthorizationStatus status: CLAuthorizationStatus)
{
    if status == .AuthorizedAlways  {

        let region = CLCircularRegion(center:    CLLocationCoordinate2D(latitude: 61.446812, longitude: 23.859914),
                  radius: 1000, identifier: "test")
        logger.info("Notification will trigger at \(region)")
        region.notifyOnEntry = true
        region.notifyOnExit = false

        let trigger = UNLocationNotificationTrigger(region: region, repeats:true)

        let content = UNMutableNotificationContent()
        content.title = "Oh Dear !"
        content.body = "It's working!"
        content.sound = UNNotificationSound.default()

        let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)

        UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
        UNUserNotificationCenter.current().add(request) {(error) in
           if let error = error {
               print("Uh oh! We had an error: \(error)")
           }
        }

    }
}
}
查看更多
登录 后发表回答