How do I make a daily notification at 8 am? (Swift 3)
I ask you to ask this, but I tried it in different ways and none of them worked. I'm currently working on an app that gives you a little notification every day when you wake up with an interesting fact. It takes these facts from the array that is set. I tried to use UNNotificationCenter, UILocalNotification, etc., but I was unable to write this function. I would be glad if you could help me with this code!
So, at 8 am the application should
- Call a function that gives it a new fact from an array and store it in UserDefaults lets you call it
newFact()
- Make a notification that appears at 8 a.m. every day, also if the phone is locked.
I would be very grateful if you could help me!
+3
source to share
1 answer
Use code below
You can call the function here
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
0
source to share