启动一个24小时定时器,当24小时结束时通知用户
我不知道在哪里看,或者我需要创建一个系统。对用户可以在我的应用中执行的对象有一个操作。当用户在对象上执行该操作时,将为该对象启动24小时倒计时。在对对象执行操作后的24小时内应通知用户。 NSTimer是我需要的吗?我不这么认为,因为即使用户要关闭应用程序或注销应用程序,或者用户离开启动定时器的视图控制器,定时器仍应运行。我真的认为它应该是在数据库中运行的东西。像24倒数状态或什么?我不知道我会如何运行计时器。启动一个24小时定时器,当24小时结束时通知用户
我考虑过一个检查系统。就像对象在Core数据和Web服务器中有一个数据库中有时间戳一样,每次用户在用户启动24小时倒计时后与用户进行交互,它都会检查时间。但是,如果用户在另一个应用程序上,如果该系统24小时运行,用户将被告知如何通知。
你描述的功能应该在服务器端。您无法确定您的应用会在24小时内启动并运行。你将如何“通知”用户取决于你想要达到的目标。我会建议使用服务器端和Push Notifications
来实现你想要的。
*“你不能确定你的应用程序将启动并运行......”*另外,OP甚至不能确定*设备*应用程序是否安装在运行中。如果不是,即使是电子邮件(最小公分母)也可能无法使用。 –
您可以在您的应用程序中使用本地通知。
//first you must register to get notification.
let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
@IBAction func NotifyUser(sender: AnyObject) {
let notify = UILocalNotification()
notify.fireDate = NSDate(timeIntervalSinceNow: 24*60*60) as Date
notify.alertBody = "Alert message body"
notify.alertAction = "Alert message action"
notify.soundName = UILocalNotificationDefaultSoundName
notify.userInfo = ["Hello": "Hi"]
UIApplication.sharedApplication.scheduleLocalNotification(notify)
guard let settings = UIApplication.sharedApplication().currentUserNotificationSettings() else { return }
if settings.types == .None {
// here we have not permission to schedule notifications, or we haven't asked yet.
return
}
}
希望它会帮助你。
您可以设置本地通知,以通知用户已经过了24小时的时间 – spassas
请参阅本地远程通知编程指南中的本地通知讨论(https://developer.apple.com/library/content /文档/ NetworkingInternet /概念/ RemoteNotificationsPG /)。 – Rob