I'm making an app that users can create "memories" that have Title, Description, Date, and a Pic. After clicking 'save', I want the app to be able to notify the user on the date he picked that his event starts. I tried this code but it's not working. il'l be glad if you could fix my code or help me find the problem :)
future = sender.date
(sender inside a UIDatePicker)
(and of course I wrote import UserNotifications
)
@IBAction func saveMemorey(_ sender: UIButton) {
// User Notification code
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "New MEmorey!"
content.subtitle = "A New Event Starts Today:"
content.body = txtTitle.text!
content.sound = UNNotificationSound.default
content.threadIdentifier = "local-notifications temp"
let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: future)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let request = UNNotificationRequest(identifier: "content", content: content, trigger: trigger)
center.add(request) { (error) in
if error != nil {
print (error)
}
}
self.navigationController?.popViewController(animated: true) // Returns to the memories page after clicking 'save'
}
AppDeligate:
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
let center = UNUserNotificationCenter.current()
let options : UNAuthorizationOptions = [.sound, .alert]
center.requestAuthorization(options: options) { (granted, error) in
if error != nil {
print (error)
}
}
center.delegate = self
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
future
related:
class AddMemoryViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
var future = Date()
var dateToSet : Double = 0.0
// connections from storyboard to the code
@IBOutlet weak var countLabel: UILabel!
@IBAction func datePickerChanged(_ sender: UIDatePicker) {
future = sender.date
//Use midnight today as the starting date
guard let today = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: Date()) else { return }
//Calculate the number of days between today and the =user's chosen day.
let difference = Calendar.current.dateComponents([.day], from: today, to: future)
guard let days = difference.day else { return }
let ess = days > 1 ? "s" : ""
if (days > 0)
{
countLabel.text = "That date is \(days) day\(ess) away."
}
if (days < 0)
{
countLabel.text = " \(abs(days)) day\(ess) since the event."
}
if (days == 0)
{
countLabel.text = " The event is today!"
}
dateToSet = Double(self.future.millisecondsSince1970)
}