首页 文章

如何在早上8点发出日常通知? (斯威夫特3)

提问于
浏览
3

我很遗憾地问你这样的事情,但我已经用五种不同的方式尝试了这一点,但没有一种方法可行 . 我正在开发一款应用程序,它可以让您每天在清醒时通知您一个有趣的事实 . 它从设置的数组中获取这些事实 . 我曾尝试使用UNNotificationCenter,UILocalNotification等,但我没有成功编写该功能 . 如果你能帮我解决这个问题,我会很高兴的!

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

  • 调用从数组中获取新事实的函数并将其存储在UserDefaults中,让我们调用它 newFact()

  • 每天早上8点发出通知,如果手机被锁定也是如此

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

1 回答

  • 0

    使用以下代码

    你可以在这里调用这个功能

    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
    

相关问题