如何在背景中的特定日期时间在swift中运行任务,无论应用程序是打开还是关闭 [英] How to Run a Task in swift on a particular date-time in background either application is on or off

查看:172
本文介绍了如何在背景中的特定日期时间在swift中运行任务,无论应用程序是打开还是关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理警报应用程序,我需要在特定时间安排警报,我使用 scheduleLocalNotification 来安排警报,并且它正常工作。 但是我需要在触发警报之前运行到我的API服务器的请求。在该请求中,我想检查从API服务器返回的一些参数,如果满足某些条件。

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.

如果任何人有一个在特定日期运行的方法 - 时间在swift
请帮帮我

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)
    }
}


推荐答案

无法通过本地通知唤醒应用程序,这仅适用于远程通知。根据通知编程指南

Waking up an app through a local notification is not possible, this is available only for remote notifications. According to the Notification Programming Guide:


远程通知到达时,系统当应用程序在后台时,通常会处理用户
的交互。它还
将通知负载传递给
应用程序:didReceiveRemoteNotification:fetchCompletionHandler:
iOS和tvOS中app代理的方法

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: 文档

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.

我不认为有一种保证的方法可以在稍后的某个时刻安排一个块执行,与当时应用程序的状态无关。根据您的具体要求和频率,您可以注册 fetch 后台模式并实现应用程序:performFetchWithCompletionHandler: to 机会性地获取并验证服务器数据。最后一点:确保您是一个负责任的后台应用程序(根据我的经验,Apple认真对待此要求)

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)

这篇关于如何在背景中的特定日期时间在swift中运行任务,无论应用程序是打开还是关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆