gpt4 book ai didi

ios - locationManager 中的 swift 和 iBeacon firedate 通知

转载 作者:行者123 更新时间:2023-11-28 08:52:01 25 4
gpt4 key购买 nike

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

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”,它会触发所有通知。我必须以某种方式找到一种在开关之后放置的方法吗?

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

最佳答案

听起来您想要做的是,如果您在过去 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)
}
}

关于ios - locationManager 中的 swift 和 iBeacon firedate 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33983671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com