如何实现地理围栏触发本地通知?

时间:2016-12-01 16:34:45

标签: ios swift ios10 geofencing

我创建了一个简单的基于地理围栏的应用程序,用于监控进入和退出地理围栏的人员的移动情况,我尝试发送有关这些事件的通知,但似乎无法正确实施

我非常感谢一些解释过的示例代码,这些代码将放在视图控制器和应用代理中。

P.s我对swift的了解有些限制,但我理解这个应用程序所需的大多数方面。 谢谢你的帮助!

编辑:

这是我创建通知的功能,我认为这是正确的。

func scheduleNotification() {
    let centre = CLLocationCoordinate2DMake(51.364730, -0.189986)
    let region = CLCircularRegion(center: centre, radius: 150, identifier: "SGS")
    region.notifyOnEntry = true

    let trigger = UNLocationNotificationTrigger(region: region, repeats: true)

    let enterContent = UNMutableNotificationContent()
    enterContent.title = "Enter"
    enterContent.body = "Entered premesis"
    enterContent.sound = UNNotificationSound.default()

    let enterRequest = UNNotificationRequest(identifier: "enterNotification", content: enterContent, trigger: trigger)

    UNUserNotificationCenter.current().removeAllPendingNotificationRequests()

    UNUserNotificationCenter.current().add(enterRequest) {(error) in
        if let error = error {
            print("Error: \(error)")
        }
    }      
}

我将此添加到didFinishLaunchingWithOptions:

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {(accepted, error) in
        if !accepted {
            print("Notification access denied.")
        }
    }

我知道我需要调用该函数,但我不知道该做什么,以便在留下地理围栏时调用它。此外,我不确定如何从视图控制器中调用它,因为它在AppDelegate中。

对不起,如果我是傻瓜,但感谢您的帮助!

1 个答案:

答案 0 :(得分:4)

您需要实施位置管理器的didEnterRegiondidExitRegion委托方法。在这些方法的实现中,您将发布本地通知。

如果您已经这样做,请确保该应用具有发布通知的正确功能,并且您已在应用代理中为该应用注册了正确的通知设置。

Ray Wenderlich在本地和远程推送通知方面有一个很好的tutorial on Geofencinghere's Apple's guide