当应用程序在ios中被杀死时,如何检测estimote beacon?

时间:2017-11-30 11:33:31

标签: ios swift3 swift4 beacon estimote

我是 estimote 信标编程的新手。我希望在应用关闭时检测 estimote 信标。当检测到信标时,然后触发特定通知。我怎么能这样做? 请给我一些建议。我做了以下代码。但我无法收到任何通知

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
 beaconManager.delegate = self
        beaconManager.startMonitoring(for: region)
        beaconManager.startRangingBeacons(in: region)
}

func beaconManager(_ manager: Any, didEnter region: CLBeaconRegion) {
        NSLog("beaconManager : didEnter Called")
        let content = UNMutableNotificationContent()
        content.title = "Beacon Detected"
        content.body = "Enter region"
        content.sound = UNNotificationSound.default()

        let trigger = UNLocationNotificationTrigger(region:region, repeats:false)
        let request = UNNotificationRequest(identifier: "Enter region", content: content, trigger: trigger)
        center.add(request) { (error) in
            if let error = error {
                print(error.localizedDescription)
            }
        }
    }
func beaconManager(_ manager: Any, didExitRegion region: CLBeaconRegion) {
            NSLog("beaconManager : didExitRegion Called")
            let content = UNMutableNotificationContent()
            content.title = "Beacon Detected"
            content.body = "Exit region"
            content.sound = UNNotificationSound.default()

            let trigger = UNLocationNotificationTrigger(region:region, repeats:false)
            let request = UNNotificationRequest(identifier: "Enter region", content: content, trigger: trigger)
            center.add(request) { (error) in
                if let error = error {
                    print(error.localizedDescription)
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

在app delegate中,在估计委托方法下编写所需的代码。示例代码如下,

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
if launchOptions["UIApplicationLaunchOptionsLocationKey"] != nil {
    beaconManager = ESTBeaconManager()
    beaconManager.delegate = self
    // don't forget the NSLocationAlwaysUsageDescription in your Info.plist
    beaconManager.requestAlwaysAuthorization()
    beaconManager.startMonitoring(for: ESTBeaconRegion(proximityUUID: ESTIMOTE_PROXIMITY_UUID, identifier: "AppRegion"))
    }
    return true
}

func beaconManager(_ manager: ESTBeaconManager, didEnter region: ESTBeaconRegion) {
    let notification = UILocalNotification()
    notification.alertBody = "Enter region"
    notification.soundName = UILocalNotificationDefaultSoundName as? String
    UIApplication.shared.presentLocalNotificationNow(notification)
}
func beaconManager(_ manager: ESTBeaconManager, didExitRegion region: ESTBeaconRegion) {
    let notification = UILocalNotification()
    notification.alertBody = "Exit region"
    notification.soundName = UILocalNotificationDefaultSoundName as? String
    UIApplication.shared.presentLocalNotificationNow(notification)
}

这是旧代码,可能没什么变化。请关注估计社区的主题以获取更多信息。您可以在此主题https://community.estimote.com/hc/en-us/articles/203253193-Pushing-notifications-from-iBeacon-when-the-app-is-in-the-background-or-killed

上找到您的问题