【问题标题】:How to implement geofence triggered local notifications?如何实现地理围栏触发的本地通知?
【发布时间】:2016-12-01 16:34:45
【问题描述】:

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

我非常感谢一些解释性的示例代码,这些示例代码将包含在视图控制器和应用程序委托中。

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 中。

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

【问题讨论】:

  • 请发布您尝试过的和无效的。

标签: ios swift ios10 geofencing


【解决方案1】:

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

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

Ray Wenderlich 在本地和远程推送通知方面拥有出色的 tutorial on Geofencinghere's Apple's guide

【讨论】:

  • 所以我应该将该代码放在 didEnterRegion/didExitRegion 中,而不是使用额外的函数?
  • 对。 didEnterRegion 和 didExitRegion 会在位置管理器注意到设备正在进入或退出其中一个设置区域时自动调用。因为那是您想要发送本地通知的时候,所以从那里创建并发送它们是有意义的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-07
  • 2016-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多