为什么我的通知没有被触发?

时间:2017-02-21 16:42:23

标签: swift push-notification

我正在尝试发送本地推送通知,或者现在称为UNLocationNotificationTrigger,但推送通知未被触发。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        let center = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
            // Enable or disable features based on authorization.
        }

        return true
    }

查看控制器

 var locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
    locationManager.delegate = self
    locationManager.requestAlwaysAuthorization()
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    locationManager.startUpdatingLocation()
    localLocations()

}

func localLocations() {
    //My current location is equal to the below coordinates, so I am in the radius
    let center = CLLocationCoordinate2D(latitude: xx.xxxxx, longitude: -xx.xxxx)
    let region = CLCircularRegion(center: center, radius: 20000, identifier: "Store")
    region.notifyOnEntry = true
    region.notifyOnExit = false
    let trigger = UNLocationNotificationTrigger(region: region, repeats: false)

    let content = UNMutableNotificationContent()
    content.body = "Grocery Store"

    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}

1 个答案:

答案 0 :(得分:0)

//My current location is equal to the below coordinates, so I am in the radius
// ...
region.notifyOnEntry = true

有你的问题。你已经在该地区。因此您无法输入它。因此通知不会触发。