How to start a task in fast transfer mode at a certain point in time in the background, either the application is turned on or off

I am working on an alarm application, I need to schedule an alarm at a specific time, I use scheduleLocalNotification to schedule alarms, and it works just the way I want. BUT I need to execute a request to my API server before starting the alarm. In this query, I want to check some parameters returned from the API server if this satisfies some condition.

If someone has a method that starts on a specific date - time is fast. Please help me for this.

  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) } } 
+5
source share
1 answer

Awakening the application through a local notification is not possible, it is available only for remote notifications. According to Notification Programming Guide :

When a remote notification arrives, the system processes the user interaction when the application is in the background. It also provides a notification payload for Application: didReceiveRemoteNotification: fetchCompletionHandler: application delegate method in iOS and tvOS

But there is still a catch; even then it is not guaranteed that the application will be launched, because, according to didReceiveRemoteNotification:fetchCompletionHandler: documentation :

However, the system does not automatically launch your application if the user has the power to quit it . In this situation, the user must restart the application or restart the device before the system tries to start the application automatically again.

I don’t think there is a guaranteed way to schedule a block for execution at some later point, regardless of the state of the application at that time. Depending on your specific requirements and frequency, you could register in the background with fetch and implement application:performFetchWithCompletionHandler: before opportunistically retrieving and checking server data . Last note: make sure you are a responsible background application (in my experience, Apple takes this requirement seriously)

+2
source

Source: https://habr.com/ru/post/1260698/


All Articles