gpt4 book ai didi

ios - 如何在应用程序打开或关闭的特定日期时间在后台快速运行任务

转载 作者:搜寻专家 更新时间:2023-10-31 08:04:20 25 4
gpt4 key购买 nike

我正在处理闹钟应用程序,我需要在特定时间安排闹钟,我使用 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.

我不认为有一种保证的方式来安排一个 block 在稍后的某个时刻执行,独立于当时应用程序的状态。根据您的具体要求和频率,您或许可以注册 fetch 后台模式并实现 application:performFetchWithCompletionHandler:opportunistically fetch and validate server data .最后注意:确保你是一个负责任的后台应用程序(根据我的经验,Apple 非常重视这个要求)

关于ios - 如何在应用程序打开或关闭的特定日期时间在后台快速运行任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40930321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com