Ios locationManager中的swift和iBeacon firedate通知

Ios locationManager中的swift和iBeacon firedate通知,ios,swift,ibeacon,locationmanager,Ios,Swift,Ibeacon,Locationmanager,下面的代码适用于通知。但是,当我尝试该应用程序时,在远近之间的通知太多了 extension AppDelegate: CLLocationManagerDelegate { func sendLocalNotificationWithMessage(message: String!) { let notification:UILocalNotification = UILocalNotification() notification.alertBody = message

下面的代码适用于通知。但是,当我尝试该应用程序时,在远近之间的通知太多了

extension AppDelegate: CLLocationManagerDelegate {
func sendLocalNotificationWithMessage(message: String!) {
    let notification:UILocalNotification = UILocalNotification()
    notification.alertBody = message
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

func locationManager(manager: CLLocationManager!,
    didRangeBeacons beacons: AnyObject[]!,
    inRegion region: CLBeaconRegion!) {
        NSLog("didRangeBeacons");
        var message:String = ""

        if(beacons.count > 0) {
            let nearestBeacon:CLBeacon = beacons[0] as CLBeacon

            switch nearestBeacon.proximity {
            case CLProximity.Far:
                message = "You are far away from the beacon"
            case CLProximity.Near:
                message = "You are near the beacon"
            case CLProximity.Immediate:
                message = "You are in the immediate proximity of the beacon"
            case CLProximity.Unknown:
                return
            }
        } else {
            message = "No beacons are nearby"
        }

        NSLog("%@", message)
        sendLocalNotificationWithMessage(message)
}
}

这是使用firedate语句的一种方式吗?比如:

localNotification.fireDate = NSDate(timeIntervalSinceNow: 900)
如果我输入“func sendlocationnotificationwithmessage”,它将激发所有通知的日期。我得想个办法把开关关上


或者可能是一个通知计数器?

听起来,如果您在过去900秒内发送了另一个通知,您要做的是延迟新通知的显示。如果这就是目标,你可以这样做:

var lastNotificationTime = 0.0

func locationManager(manager: CLLocationManager!,
  didRangeBeacons beacons: AnyObject[]!,
  inRegion region: CLBeaconRegion!) {
    NSLog("didRangeBeacons");
    var beaconVisible = false
    var proximity = CLProximity.Unknown
    if(beacons.count > 0) {
      beaconVisible = true
      let nearestBeacon:CLBeacon = beacons[0] as CLBeacon
      proximity = nearestBeacon.proximity
    }
    else {
      beaconVisible = false
    }

    if (NSDate.timeIntervalSinceReferenceDate() - lastNotificationTime > 900) {
      lastNotificationTime = NSDate.timeIntervalSinceReferenceDate()
      var message:String = ""
      if beaconVisible {
        switch proximity {
        case CLProximity.Far:
          message = "You are far away from the beacon"
        case CLProximity.Near:
          message = "You are near the beacon"
        case CLProximity.Immediate:
          message = "You are in the immediate proximity of the beacon"
        case CLProximity.Unknown:
          return
        }
      }
      else {
        message = "No beacons are nearby";
      }
      NSLog("%@", message)
      sendLocalNotificationWithMessage(message)
    }
  }

听起来,如果您在过去900秒内发送了另一个通知,那么您要做的是延迟新通知的显示。如果这就是目标,你可以这样做:

var lastNotificationTime = 0.0

func locationManager(manager: CLLocationManager!,
  didRangeBeacons beacons: AnyObject[]!,
  inRegion region: CLBeaconRegion!) {
    NSLog("didRangeBeacons");
    var beaconVisible = false
    var proximity = CLProximity.Unknown
    if(beacons.count > 0) {
      beaconVisible = true
      let nearestBeacon:CLBeacon = beacons[0] as CLBeacon
      proximity = nearestBeacon.proximity
    }
    else {
      beaconVisible = false
    }

    if (NSDate.timeIntervalSinceReferenceDate() - lastNotificationTime > 900) {
      lastNotificationTime = NSDate.timeIntervalSinceReferenceDate()
      var message:String = ""
      if beaconVisible {
        switch proximity {
        case CLProximity.Far:
          message = "You are far away from the beacon"
        case CLProximity.Near:
          message = "You are near the beacon"
        case CLProximity.Immediate:
          message = "You are in the immediate proximity of the beacon"
        case CLProximity.Unknown:
          return
        }
      }
      else {
        message = "No beacons are nearby";
      }
      NSLog("%@", message)
      sendLocalNotificationWithMessage(message)
    }
  }