使用 API 调用的本地通知
local notification with API call
我收到了一个应用程序,其中有一些本地通知。即使应用程序处于后台,它们也可以与触发器一起正常工作。
但现在我在该通知中只有一些静态文本,然后单击它并在应用程序上打开它,它 api 调用服务器,然后我在服务器上取回我需要的名称和内容.
我想做的是,当通知被触发时,它会调用 Api 并获取内容。所以我的通知可以写成类似 "This is X" 的内容,X 被替换为服务器的内容。
这是我正在使用的 iBeacons,所以当我靠近 iBeacon 时,我会收到通知。
所以我假设您有一些调用 didRangeBeacons
:
的 iBeacon 管理器
func locationManager(manager: CLLocationManager, didRangeBeacons clBeacons: [AnyObject], inRegion region: CLBeaconRegion) {
// schedule local notification
}
然后您在其中安排本地通知。您需要做的就是先进行服务器调用,并在完成后安排本地通知。
您需要记住,当用户进入信标区域时,您的应用程序可能处于后台(甚至被终止),因此您需要在 didEnterRegion
被调用时开始测距:
func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {
// start ranging for beacons
}
使用beginBackgroundTaskWithExpirationHandler , to make the API call in the background. See this thread for detail info: Proper use of beginBackgroundTaskWithExpirationHandler。在此后台任务中,您可以执行 API 请求并显示带有请求结果的通知。
我收到了一个应用程序,其中有一些本地通知。即使应用程序处于后台,它们也可以与触发器一起正常工作。
但现在我在该通知中只有一些静态文本,然后单击它并在应用程序上打开它,它 api 调用服务器,然后我在服务器上取回我需要的名称和内容.
我想做的是,当通知被触发时,它会调用 Api 并获取内容。所以我的通知可以写成类似 "This is X" 的内容,X 被替换为服务器的内容。
这是我正在使用的 iBeacons,所以当我靠近 iBeacon 时,我会收到通知。
所以我假设您有一些调用 didRangeBeacons
:
func locationManager(manager: CLLocationManager, didRangeBeacons clBeacons: [AnyObject], inRegion region: CLBeaconRegion) {
// schedule local notification
}
然后您在其中安排本地通知。您需要做的就是先进行服务器调用,并在完成后安排本地通知。
您需要记住,当用户进入信标区域时,您的应用程序可能处于后台(甚至被终止),因此您需要在 didEnterRegion
被调用时开始测距:
func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {
// start ranging for beacons
}
使用beginBackgroundTaskWithExpirationHandler , to make the API call in the background. See this thread for detail info: Proper use of beginBackgroundTaskWithExpirationHandler。在此后台任务中,您可以执行 API 请求并显示带有请求结果的通知。