如何在Swift中每天仅显示一次信标通知?

时间:2018-09-15 20:53:53

标签: swift beacon

在检测到信标信号时,我的信标通知会随时触发。如何限制每天只接收一次通知?

我实施了日期检查,但这没有帮助。 if / else检查工作正常,但是忽略该检查会触发信标通知

任何解决方法的建议都将受到赞赏!

这是我的代码段:

class ViewController: UIViewController, CLLocationManagerDelegate {

    let locationManager = CLLocationManager()

    let date = Date()
    let calendar = Calendar.current

    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
        locationManager.desiredAccuracy = 10
        locationManager.distanceFilter = 100
        locationManager.startUpdatingLocation()


        let year = calendar.component(.year, from: date)
        let month = calendar.component(.month, from: date)
        let day = calendar.component(.day, from: date)
        let currentDate = "\(year)\(month)\(day)"
        var savedDate = UserDefaults.standard.string(forKey: "savedDate") ?? "12345"

        print(currentDate)
        print(savedDate)

        if (savedDate == currentDate){
            print("same date - no action")
        } else {
            savedDate = currentDate
            UserDefaults.standard.set(savedDate, forKey: "savedDate")
            print(savedDate + " execute program")

            let beaconRegion = CLBeaconRegion(proximityUUID: UUID(uuidString: "13D9F4C7-A68D-46F4-8D35-4BA7F64BC417")!, identifier: "estimote")
            beaconRegion.notifyOnEntry = true
            beaconRegion.notifyOnExit = false

            let content = UNMutableNotificationContent()
            content.title = " Daily beacon check! "
            content.subtitle = "Receive a new info every day!"
            content.body = "ONLY ONCE A DAY WE SUPPLY INFO!"
            content.sound = .default
            content.badge = 1

            let trigger = UNLocationNotificationTrigger(region: beaconRegion, repeats: true)
            let identifier = "estimote"
            let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)
            UNUserNotificationCenter.current().removeAllPendingNotificationRequests()

            UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
            UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error) in
            })

            self.locationManager.startRangingBeacons(in: beaconRegion)

        }
    }

1 个答案:

答案 0 :(得分:2)

通过设置

let trigger = UNLocationNotificationTrigger(region: beaconRegion, repeats: true)
let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)

您基本上是在告诉程序关闭,并在满足beaconRegion条件时自动发送通知。万一您始终需要通知并让系统为您处理通知,这很舒服。但是您将无法干预,因为它会自动运行。

您可以做的是手动处理通知,并检查是否应在每次检测到信标时发送通知。为此,请使用locationManager(didRangeBeacons beacons:in region:)委托函数:

let locationManager = CLLocationManager()
override func viewDidLoad() {
    super.viewDidLoad()

    // Setup beacon tracking once
    locationManager.delegate = self
    locationManager.requestAlwaysAuthorization()
    locationManager.desiredAccuracy = 10
    locationManager.distanceFilter = 100
    locationManager.startUpdatingLocation()
    let beaconRegion = CLBeaconRegion(proximityUUID: UUID(uuidString: "13D9F4C7-A68D-46F4-8D35-4BA7F64BC417")!, identifier: "estimote")
    beaconRegion.notifyOnEntry = true
    beaconRegion.notifyOnExit = false

    // Start looking for beacons
    self.locationManager.startRangingBeacons(in: beaconRegion)
}

// Handle beacons in range
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
    let now = Date()
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyyMMdd"
    let nowString = formatter.string(from: now)

    // Check if notified ever (lastTime != nil) and if notified today
    if let lastTime = UserDefaults.standard.string(forKey: "savedDate"), lastTime == nowString {
        // Already notified today, skip
        print("same date - no action")
        return
    }

    // Your notification code
    let content = UNMutableNotificationContent()
    content.title = " Daily beacon check! "
    content.subtitle = "Receive a new info every day!"
    content.body = "ONLY ONCE A DAY WE SUPPLY INFO!"
    content.sound = UNNotificationSound.default()
    content.badge = 1

    let identifier = "estimote"
    let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: nil)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error) in
        if error != nil {
          print("Error showing notification: \(error!.localizedDescription)")
        } else {
          print("Notification shown")
        }
    })

    UserDefaults.standard.set(nowString, forKey: "savedDate")
}