Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Run a Task in swift on a particular date-time in background either application is on or off

I am working on alarm application, i need to schedule alarm on specific time, I use scheduleLocalNotification for scheduling alarms and it's working fine as i want. BUT I need to run to a request to my API server before triggering alarm. In that request I want to check some parameters returning from API server, If that satisfies some condition.

If any one have a method that run on a particular date - time in swift Please help me for that

 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)
    }
}
like image 286
Pushpendra Avatar asked Oct 29 '22 16:10

Pushpendra


1 Answers

Waking up an app through a local notification is not possible, this is available only for remote notifications. According to the 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

But there is still a catch; even then it is not guaranteed that the app will be launched since, according to 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.

I don't think there is a guaranteed way to schedule a block for execution in some later moment, independently from the state of the app at that time. Depending on your specific requirements and frequency, you could perhaps register for the fetch background mode and implement application:performFetchWithCompletionHandler: to opportunistically fetch and validate server data. Last note: make sure that you are a responsible background app (from my experience Apple takes this requirement seriously)

like image 188
spassas Avatar answered Nov 14 '22 16:11

spassas