I have set up a local notification to fire. I am wondering how to get it to repeat every week at the same time e.g., 9 AM on Monday.
Here's my code so far:
#IBAction func scheduleLocal(sender: UIButton) {
guard let settings = UIApplication.sharedApplication().currentUserNotificationSettings() else { return
}
if settings.types == .None {
let ac = UIAlertController(title: "Cant Schedule", message: "No Permission", preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
return
}
let notification = UILocalNotification()
notification.fireDate = NSDate()
notification.alertBody = "Come Exercise"
notification.alertAction = "Exercise Time"
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["customField1": "w00t"]
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
and in viewDidLoad:
let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
There is a repeatInterval you can set on the local notification that takes an NSCalendarUnit as its type. You can read more about the different available calendar units here https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/#//apple_ref/c/tdef/NSCalendarUnit
You would likely be looking for NSCalendarUnitWeekOfYear
So you could just add the following to your code for your notification
notification.repeatInterval = NSCalendarUnit.WeekOfYear
Your code is right.
But:
"Each app on a device is limited to 64 scheduled local notifications."
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/WhatAreRemoteNotif.html
You can schedule the local notifications and recreate them each time the user open the app. Or use remote notifications instead.
Related
I need to remove an event with certain/specific title, I hope that I can delete/remove the event based on the eventID/Identifier. but I don't know how to do that in code. I don't know how to give identifier to the event and remove it based on their identifier/title.
here is the code I use to save the event:
let eventStore = EKEventStore()
let newEvent = EKEvent(eventStore: eventStore)
newEvent.calendar = eventStore.defaultCalendarForNewEvents
newEvent.title = self.eventNameTextField.text ?? "Some Event Name"
newEvent.startDate = timeDatePicker.date
newEvent.endDate = endTimeDatePicker.date
newEvent.notes = "Ini adalah catatan"
newEvent.location = "Jalan Sunda kelapa no.60"
let eventAlarm = EKAlarm(relativeOffset: -60 * 10) // 10 minutes before the start date
newEvent.alarms = [eventAlarm]
do {
try eventStore.save(newEvent, span: .thisEvent)
print("Event has been saved")
} catch {
let alert = UIAlertController(title: "Event could not be saved", message: (error as NSError).localizedDescription, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(OKAction)
self.present(alert, animated: true, completion: nil)
}
I know that I can use evenStore.remove() , but that method needs EKEvent instance. I don't understand how to remove a specific event if using that method, it will be easier if I can remove the event based on their identifier
Actually an EKEvent instance has a get-only attribute called eventIdentifier. You can't modify this identifier, but you can get it after you save the event. So:
do {
try eventStore.save(newEvent, span: .thisEvent)
let id = newEvent.eventIdentifier ?? "NO ID"
//Save your ID in your database or anywhere else so you can retrieve the event later
print("Event has been saved with id \(id)")
} catch {
let alert = UIAlertController(title: "Event could not be saved", message: (error as NSError).localizedDescription, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(OKAction)
self.present(alert, animated: true, completion: nil)
}
Then you can get the event using its identifier
let event = eventStore.event(withIdentifier: id)
and then pass this EKEvent to eventStore.remove()
Currently I have the following code, which is essentially asking the user for permission to send notifications, and then checking to see if notifications are enabled:
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: { (granted, error) in
if granted {
} else {
}
})
let notifType = UIApplication.shared.currentUserNotificationSettings?.types
if notifType?.rawValue == 0 {
print("being called")
let alert = UIAlertController(title: "Notifications are disabled for this app.", message: "Please enable notifications for the app to work properly. This can be done by going to Settings > Notifications > NAME HERE and allow notifications.", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil))
show(alert, sender: self)
} else {
//enabled
}
The code works, however, it checks if notifications are enabled before the user can select "yes" or "no". Thus, no matter what is selected, the dialog box pops up. Is there a way I can wait to check for authorization status until the user selects "yes" or "no"?
You can move the check into the callback, so authorization is checked first:
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: { (granted, error) in
if (granted) {
// Alert here
} else {
// Or here
}
})
I need your help for a project.
I have 3 variables, one for the day , another for the month and last for the year. Like that :
var year = 2017
var month = 06
var day = 19
I want to send a notification even if the app is close when we are at the date of these variable, but i'm not really good with Calendar and Date. I just made this app for the moment.
let myNotification = Notification.Name(rawValue:"MyNotification")
override func viewDidLoad() {
super.viewDidLoad()
let nc = NotificationCenter.default
nc.addObserver(forName:myNotification, object:nil, queue:nil, using:catchNotification)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let nc = NotificationCenter.default
nc.post(name:myNotification,
object: nil,
userInfo:["message":"Hello there!", "date":Date()])
}
func catchNotification(notification:Notification) -> Void {
print("Catch notification")
guard let userInfo = notification.userInfo,
let message = userInfo["message"] as? String,
let date = userInfo["date"] as? Date else {
print("No userInfo found in notification")
return
}
let alert = UIAlertController(title: "Notification!",
message:"\(message) received at \(date)",
preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
Thank you in advance
You need to set up a local notification and use a UNCalendarNotificationTrigger to fire it at a specific date.
let dateComponents = DateComponents(year: year, month: month, day: day)
let yourFireDate = Calendar.current.date(from: dateComponents)
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey:
"Your notification title", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "Your notification body", arguments: nil)
content.categoryIdentifier = "Your notification category"
content.sound = UNNotificationSound.default()
content.badge = 1
let dateComponents = Calendar.current.dateComponents(Set(arrayLiteral: Calendar.Component.year, Calendar.Component.month, Calendar.Component.day), from: yourFireDate)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let request = UNNotificationRequest(identifier: "Your notification identifier", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
if let error = error {
//handle error
} else {
//notification set up successfully
}
}
In swift 3 - 4 use this.
func scheduleLocal() {
let dateComponents = DateComponents(year: 2019, month: 1, day: 17, hour: 10, minute: 20)
let yourFireDate = Calendar.current.date(from: dateComponents)
let notification = UILocalNotification()
notification.fireDate = yourFireDate
notification.alertBody = "Hey you! Yeah you! Swipe to unlock!"
notification.alertAction = "be awesome!"
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["CustomField1": "w00t"]
UIApplication.shared.scheduleLocalNotification(notification)
}
I've created an appointment reminder app for customers but a few have said they would like the app to give them an earlier notification say one day in advance (24 hours) as well as at the time of the appointment, but I'm unsure how I can edit my code to do this.
Here is my working code that shows the appointment at the chosen time on the date picker:
import UIKit
import EventKit
class RemindersViewController: UIViewController {
#IBOutlet weak var reminderText: UITextField!
#IBOutlet weak var myDatePicker: UIDatePicker!
#IBOutlet weak var activityIndicator: UIActivityIndicatorView!
let appDelegate = UIApplication.shared.delegate
as! AppDelegate
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func setReminder(_ sender: AnyObject) {
if reminderText.text == "" {
// Create the alert controller
let alertController = UIAlertController(title: "Information Needed", message: "Please type in your treatment and select the correct date and time you wish to be reminded about before pressing the Create Appointment Reminder button.", preferredStyle: .alert)
// Create the actions
let okAction = UIAlertAction(title: "Got It", style: UIAlertActionStyle.default) {
UIAlertAction in
NSLog("OK Pressed")
}
// Add the actions
alertController.addAction(okAction)
// Present the controller
self.present(alertController, animated: true, completion: nil)
} else {
activityIndicator.startAnimating()
let eventStore = EKEventStore()
eventStore.requestAccess(
to: EKEntityType.reminder, completion: {(granted, error) in
if !granted {
print("Access to store not granted")
print(error!.localizedDescription)
} else {
print("Access granted")
self.createReminder(in: eventStore)
}
})
}
self.reminderText.resignFirstResponder()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func createReminder(in eventStore: EKEventStore) {
let reminder = EKReminder(eventStore: eventStore)
reminder.title = reminderText.text! + " " + "(Pose Beauty Salon)"
reminder.calendar =
eventStore.defaultCalendarForNewReminders()
let date = myDatePicker.date
let alarm = EKAlarm(absoluteDate: date)
reminder.addAlarm(alarm)
do {
try eventStore.save(reminder,
commit: true)
} catch let error {
print("Reminder failed with error \(error.localizedDescription)")
}
// Create the alert controller
let alertController = UIAlertController(title: "Reminder Created Successfully", message: "Your \(reminderText.text!) appointment reminder at Pose Beauty Salon has been successfully created in your iPhone Reminders app. Thank You!", preferredStyle: .alert)
// Create the actions
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
UIAlertAction in
NSLog("OK Pressed")
self.reminderText.text = ""
self.activityIndicator.stopAnimating()
}
// Add the actions
alertController.addAction(okAction)
// Present the controller
self.present(alertController, animated: true, completion: nil)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
reminderText.endEditing(true)
}
}
I've changed my code as follows but I only get the reminder saved once:
func createReminder(in eventStore: EKEventStore) {
let reminder = EKReminder(eventStore: eventStore)
let secondReminder = EKReminder(eventStore: eventStore)
reminder.title = reminderText.text! + " " + "(Pose Beauty Salon)"
reminder.calendar =
eventStore.defaultCalendarForNewReminders()
secondReminder.title = reminderText.text! + " Tomorrow " + "(Pose Beauty Salon)"
secondReminder.calendar =
eventStore.defaultCalendarForNewReminders()
// let date = myDatePicker.date
let actualDate = myDatePicker.date
let earlyReminderDate = actualDate.addingTimeInterval(-3600*24)
//let alarm = EKAlarm(absoluteDate: date)
let alarm = EKAlarm(absoluteDate: actualDate)
let secondAlarm = EKAlarm(absoluteDate: earlyReminderDate)
reminder.addAlarm(alarm)
secondReminder.addAlarm(secondAlarm)
It seems that eventStore.defaultCalendarForNewReminders() doesn't allow for multiple alarms.
You can achieve this behaviour if you save the reminder to the calendar app instead.
I made some changes to your code to do this, hopefully this is useful:
Updated the access request
let eventStore = EKEventStore()
eventStore.requestAccess(
to: EKEntityType.event, completion: {(granted, error) in
if !granted {
print("Access to store not granted")
print(error!.localizedDescription)
} else {
print("Access granted")
self.createReminder(in: eventStore)
}
})
Create an EKEvent instead of EKReminder and open the EKEventEditViewController
func createReminder(in eventStore: EKEventStore) {
let reminder = EKEvent(eventStore: eventStore)
reminder.title = reminderText.text! + " " + "(Pose Beauty Salon)"
reminder.calendar =
eventStore.defaultCalendarForNewEvents
let date = myDatePicker.date
let alarm = EKAlarm(absoluteDate: date)
reminder.addAlarm(alarm)
let earlierDate = date.addingTimeInterval(-3600*24)
let earlierAlarm = EKAlarm(absoluteDate: earlierDate)
reminder.addAlarm(earlierAlarm)
reminder.startDate = date
reminder.endDate = date.addingTimeInterval(3600)
do {
try eventStore.save(reminder, span: .thisEvent, commit: true)
} catch let error {
print("Reminder failed with error \(error.localizedDescription)")
return
}
// Create the alert controller
let alertController = UIAlertController(title: "Reminder Created Successfully", message: "Your \(reminderText.text!) appointment reminder at Pose Beauty Salon has been successfully created in your iPhone Reminders app. Thank You!", preferredStyle: .alert)
// Create the actions
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
UIAlertAction in
NSLog("OK Pressed")
self.reminderText.text = ""
self.activityIndicator.stopAnimating()
}
// Add the actions
alertController.addAction(okAction)
// Present the controller
self.present(alertController, animated: true, completion: nil)
}
Added delegate method from EKEventEditViewDelegate
func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) {
switch action
{
case .saved:
let alertController = UIAlertController(title: "Reminder Created Successfully", message: "Your \(reminderText.text!) appointment reminder at Pose Beauty Salon has been successfully created in your iPhone Reminders app. Thank You!", preferredStyle: .alert)
// Create the actions
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
UIAlertAction in
NSLog("OK Pressed")
self.reminderText.text = ""
self.activityIndicator.stopAnimating()
}
// Add the actions
alertController.addAction(okAction)
// Present the controller
self.present(alertController, animated: true, completion: nil)
default:
self.dismiss(animated: true, completion: nil)
}
}
You could create two separate reminders with all same data but with different dates like this:
let actualDate = myDatePicker.date
let earlyReminderDate = actualDate.addingTimeInterval(-3600*24)
I have a local alert set to fire off at a predesignated time (30 seconds). What I want to do is fire off an alert at 20 seconds. This is my relevant appDelegate code:
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
// Pass the "firing" event onto the notification manager
timerNotificationManager.timerFired()
if application.applicationState == .Active {
//let alert = UIAlertController(title: "NotifyTimely", message: "Your time is up", preferredStyle: .Alert)
// Handler for each of the actions
let actionAndDismiss = {
(action: String?) -> ((UIAlertAction!) -> ()) in
return {
_ in
self.timerNotificationManager.handleActionWithIdentifier(action)
self.window?.rootViewController?.dismissViewControllerAnimated(true, completion: nil)
}
}
/*
alert.addAction(UIAlertAction(title: "Dismiss", style: .Cancel, handler: actionAndDismiss(nil)))
alert.addAction(UIAlertAction(title: "Restart", style: .Default, handler: actionAndDismiss(restartTimerActionString)))
alert.addAction(UIAlertAction(title: "Snooze", style: .Destructive, handler: actionAndDismiss(snoozeTimerActionString)))
window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
*/
var ourAlert = UIAlertView(title: "Time Alert", message: "You have been active for 20 seconds!", delegate: nil, cancelButtonTitle: "Dismiss")
ourAlert.show()
self.finalAlert()
}
}
func finalAlert() {
let alert = UIAlertView()
alert.title = "Final Timer Alert"
alert.message = "You have been active for 20 seconds. Your ride is now being charged."
alert.addButtonWithTitle("OK")
alert.show()
}
Now I have seen this answer How can I use NSTimer in Swift?
But I don't want the finalAlert function to kick off immediately. I want it to fire off 10s after the initial alert. How do I get NSTimer to wait 10 seconds to fire the alert or is there a better way to wait?
wait 10 seconds
It's unclear to me what you think is wrong with the behavior of NSTimer, but in any case the simplest way to express the notion "do this, 10 seconds from now" is to use GCD's dispatch_after. And the easiest way to do that is as I encapsulate it here: dispatch_after - GCD in swift?
NSTimer is literally meant to not fire immediately. You start the timer with a time interval of when you want it to fire, I guess in your case 10 seconds.
//first parameter is a time interval in seconds target is self and selector is
//finalAlert, which means after 10 seconds it will call self.finalAlert
//userInfo is nil, because you aren't passing any additional info
//and repeats is false, because you only want the timer to run once.
let timer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: "finalAlert", userInfo: nil, repeats: false)