【问题标题】:Start a 24 hour timer that notifies user when 24 hours is up启动 24 小时计时器,当 24 小时结束时通知用户
【发布时间】:2016-11-29 15:05:48
【问题描述】:

我不知道去哪里寻找,或者我需要什么来创建一个系统。用户可以在我的应用程序中执行的对象上的操作。当用户对某个对象执行该操作时,将为该对象启动 24 小时倒计时。自对对象执行操作后 24 小时后,应通知用户。我在这里需要一个 NSTimer 吗?我真的不这么认为,因为即使用户要关闭应用程序或注销应用程序,或者用户离开启动计时器的视图控制器,计时器也应该运行。我真的认为它应该是在数据库中运行的东西。比如24倒计时状态什么的?我不知道我怎么会有一个正在运行的计时器。

我考虑过检查系统。就像该对象在核心数据中有一个时间戳,在 Web 服务器中有一个数据库,并且每次用户在用户启动 24 小时倒计时后与该对象交互时,它都会检查时间。但是,如果用户在另一个应用程序上,如果该系统 24 小时已到,将如何通知用户。

【问题讨论】:

标签: ios swift database core-data nstimer


【解决方案1】:

您描述的功能应该在服务器端。您无法确定您的应用程序会在 24 小时内启动并运行。您将如何“通知”用户取决于您想要实现的目标。我建议使用服务器端和Push Notifications 来实现您想要的。

【讨论】:

  • “您无法确定您的应用程序是否会启动并运行...” 此外,OP 甚至无法确定 设备安装的应用程序将运行。如果不是这样,即使是电子邮件(最低公分母)也可能无法使用。
【解决方案2】:

您可以在应用中使用本地通知。

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

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 2012-06-20
    • 2021-04-20
    • 1970-01-01
    相关资源
    最近更新 更多