【问题标题】:swift and iBeacon firedate notification in locationManagerlocationManager 中的 swift 和 iBeacon 触发通知
【发布时间】:2015-11-29 14:06:16
【问题描述】:

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

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 sendLocalNotificationWithMessage”,它将触发所有通知。我必须以某种方式在切换之后放置?

或者也许是通知计数器?

【问题讨论】:

    标签: ios swift ibeacon locationmanager


    【解决方案1】:

    如果您在过去 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)
        }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      • 2017-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多