【问题标题】:How to make a daily notification at 8 am? (Swift 3)如何在早上 8 点进行每日通知? (斯威夫特 3)
【发布时间】:2017-04-05 16:56:43
【问题描述】:

很抱歉向您提出这样的要求,但我已经尝试了五种不同的方法,但都没有奏效。我目前正在开发一款应用程序,该应用程序每天都会在您醒来时为您提供带有有趣事实的小通知。它从设置的数组中获取这些事实。我尝试使用 UNNotificationCenter、UILocalNotification 等,但我没有成功编写该函数。如果你能帮我解决这段代码,我会很高兴!

所以,早上 8 点,应用程序必须

  1. 调用从数组中提供新事实的函数并将其存储在 UserDefaults 中,我们称之为newFact()
  2. 每天早上 8 点发出通知,即使手机被锁定

如果你能帮助我,我将非常感激!

【问题讨论】:

    标签: ios swift swift3 notifications


    【解决方案1】:

    使用下面的代码

    你可以在这里调用函数

    let fact = self.newfact()
    UserDefaults.standard.set(fact,forKey: "fact")
    
    
    var dateComp:NSDateComponents = NSDateComponents()
                dateComp.year = 2017;
                dateComp.month = 04;
                dateComp.day = 05;
                dateComp.hour = 08;
                dateComp.minute = 00;
                dateComp.timeZone = NSTimeZone.systemTimeZone()
    
            var calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
            var date:NSDate = calender.dateFromComponents(dateComp)!
    
            var notification:UILocalNotification = UILocalNotification()
            notification.category = "Daily alarm "
            notification.alertBody = fact
            notification.fireDate = date
            notification.repeatInterval = NSCalendarUnit.CalendarUnitDay
    

    【讨论】:

    • 每次触发通知时显示新文本怎么样?
    • 您可以用作可以按日期查找的变量假设您有字典 {date:body} notification.alertbody = dict[date]
    • 已经谢谢了,但问题是我需要调用该函数,因为它首先需要一个随机数,然后将其作为索引插入。我必须将它保存到 UserDefaults。
    • 在哪里可以插入引用我的函数的代码行? ://
    • 你能展示你的功能以及你需要发送到警报正文的内容吗
    猜你喜欢
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 2017-05-26
    • 2016-11-23
    相关资源
    最近更新 更多