如何在后台的特定日期时间 运行 swift 中的任务,无论应用程序是打开还是关闭
How to Run a Task in swift on a particular date-time in background either application is on or off
我正在开发闹钟应用程序,我需要在特定时间安排闹钟,我使用 scheduleLocalNotification
来安排闹钟,它工作正常,如我所愿。 但是 我需要 运行 在触发警报之前向我的 API 服务器发出请求。在该请求中,我想检查从 API 服务器返回的一些参数,如果满足某些条件。
如果有人有方法 运行 在特定日期 - 时间 swift
请帮助我
func addAlarm (newAlarm: Alarm) {
// Create persistent dictionary of data
var alarmDictionary = NSUserDefaults.standardUserDefaults().dictionaryForKey(ALARMS_KEY) ?? Dictionary()
// Copy alarm object into persistent data
alarmDictionary[newAlarm.UUID] = newAlarm.toDictionary()
// Save or overwrite data
NSUserDefaults.standardUserDefaults().setObject(alarmDictionary, forKey: ALARMS_KEY)
scheduleNotification(newAlarm, category: "ALARM_CATEGORY")
scheduleNotification(newAlarm, category: "FOLLOWUP_CATEGORY")
}
/* NOTIFICATION FUNCTIONS */
func scheduleNotification (alarm: Alarm, category: String) {
let notification = UILocalNotification()
notification.category = category
notification.repeatInterval = NSCalendarUnit.Day
switch category {
case "ALARM_CATEGORY":
notification.userInfo = ["UUID": alarm.UUID]
notification.alertBody = "Time to wake up!"
notification.fireDate = alarm.wakeup
notification.timeZone = NSTimeZone.localTimeZone()
notification.soundName = "loud_alarm.caf"
break
case "FOLLOWUP_CATEGORY":
notification.userInfo = ["UUID": alarm.followupID]
notification.alertBody = "Did you arrive yet?"
notification.fireDate = alarm.arrival
notification.timeZone = NSTimeZone.localTimeZone()
notification.soundName = UILocalNotificationDefaultSoundName
break
default:
print("ERROR SCHEDULING NOTIFICATION")
return
}
print("Notification=\(notification)")
// For debugging purposes
if alarm.isActive {
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
}
无法通过本地通知唤醒应用程序,这仅适用于远程通知。根据 Notification Programming Guide:
When a remote notification arrives, the system handles user
interactions normally when the app is in the background. It also
delivers the notification payload to the
application:didReceiveRemoteNotification:fetchCompletionHandler:
method of the app delegate in iOS and tvOS
但是还有一个问题;即使那样,也不能保证该应用程序会启动,因为根据 didReceiveRemoteNotification:fetchCompletionHandler:
documentation:
However, the system does not automatically launch your app if the user
has force-quit it. In that situation, the user must relaunch your app
or restart the device before the system attempts to launch your app
automatically again.
我不认为有一种保证的方式来安排一个块在稍后的某个时刻执行,独立于当时应用程序的状态。根据您的具体要求和频率,您或许可以注册 fetch
后台模式并实现 application:performFetchWithCompletionHandler:
到 opportunistically fetch and validate server data。最后注意:确保你是一个负责任的后台应用程序(根据我的经验,Apple 非常重视这个要求)
我正在开发闹钟应用程序,我需要在特定时间安排闹钟,我使用 scheduleLocalNotification
来安排闹钟,它工作正常,如我所愿。 但是 我需要 运行 在触发警报之前向我的 API 服务器发出请求。在该请求中,我想检查从 API 服务器返回的一些参数,如果满足某些条件。
如果有人有方法 运行 在特定日期 - 时间 swift 请帮助我
func addAlarm (newAlarm: Alarm) {
// Create persistent dictionary of data
var alarmDictionary = NSUserDefaults.standardUserDefaults().dictionaryForKey(ALARMS_KEY) ?? Dictionary()
// Copy alarm object into persistent data
alarmDictionary[newAlarm.UUID] = newAlarm.toDictionary()
// Save or overwrite data
NSUserDefaults.standardUserDefaults().setObject(alarmDictionary, forKey: ALARMS_KEY)
scheduleNotification(newAlarm, category: "ALARM_CATEGORY")
scheduleNotification(newAlarm, category: "FOLLOWUP_CATEGORY")
}
/* NOTIFICATION FUNCTIONS */
func scheduleNotification (alarm: Alarm, category: String) {
let notification = UILocalNotification()
notification.category = category
notification.repeatInterval = NSCalendarUnit.Day
switch category {
case "ALARM_CATEGORY":
notification.userInfo = ["UUID": alarm.UUID]
notification.alertBody = "Time to wake up!"
notification.fireDate = alarm.wakeup
notification.timeZone = NSTimeZone.localTimeZone()
notification.soundName = "loud_alarm.caf"
break
case "FOLLOWUP_CATEGORY":
notification.userInfo = ["UUID": alarm.followupID]
notification.alertBody = "Did you arrive yet?"
notification.fireDate = alarm.arrival
notification.timeZone = NSTimeZone.localTimeZone()
notification.soundName = UILocalNotificationDefaultSoundName
break
default:
print("ERROR SCHEDULING NOTIFICATION")
return
}
print("Notification=\(notification)")
// For debugging purposes
if alarm.isActive {
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
}
无法通过本地通知唤醒应用程序,这仅适用于远程通知。根据 Notification Programming Guide:
When a remote notification arrives, the system handles user interactions normally when the app is in the background. It also delivers the notification payload to the application:didReceiveRemoteNotification:fetchCompletionHandler: method of the app delegate in iOS and tvOS
但是还有一个问题;即使那样,也不能保证该应用程序会启动,因为根据 didReceiveRemoteNotification:fetchCompletionHandler:
documentation:
However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.
我不认为有一种保证的方式来安排一个块在稍后的某个时刻执行,独立于当时应用程序的状态。根据您的具体要求和频率,您或许可以注册 fetch
后台模式并实现 application:performFetchWithCompletionHandler:
到 opportunistically fetch and validate server data。最后注意:确保你是一个负责任的后台应用程序(根据我的经验,Apple 非常重视这个要求)