UserNotification in 3 days then repeat every day/hour - iOS 10 - swift

UILocalNotification has been depreciated so I would like to update my code to the UserNotification framework:
let alertDays = 3.0
let alertSeconds = alertDays * 24.0 * 60.0 * 60.0
let localNotification:UILocalNotification = UILocalNotification()
localNotification.alertAction = "Reminder"
localNotification.alertTitle = "Reminder Title"
localNotification.alertBody = "Reminder Message"
localNotification.fireDate = Foundation.Date(timeIntervalSinceNow: alertSeconds)
localNotification.repeatInterval = .day
UIApplication.shared().scheduleLocalNotification(localNotification)
How can I set a similar daily or hourly repeat with the UserNotification framework after waiting for the initial notification?
let alertDays = 3.0
let alertSeconds = alertDays * 24.0 * 60.0 * 60.0
let content: UNMutableNotificationContent = UNMutableNotificationContent()
content.title = "Reminder Title"
content.subtitle = "Reminder Subtitle"
content.body = "Reminder Message"
let calendar = Calendar.current
let alarmTime = Foundation.Date(timeIntervalSinceNow: alertSeconds)
let alarmTimeComponents = calendar.components([.day, .hour, .minute], from: alarmTime)
let trigger = UNCalendarNotificationTrigger(dateMatching: alarmTimeComponents, repeats: true)
let request = UNNotificationRequest(identifier: workoutAlarmIdentifier,
content: content,
trigger: trigger)
UNUserNotificationCenter.current().add(request)
{
(error) in // ...
}

It seems like this is not supported, but to make a workaround you could use:
let alertDays = 3.0
let daySeconds = 86400
let alertSeconds = alertDays * daySeconds
let content: UNMutableNotificationContent = UNMutableNotificationContent()
content.title = "Reminder Title"
content.subtitle = "Reminder Subtitle"
content.body = "Reminder Message"
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: (alertSeconds), repeats: false)
let request = UNNotificationRequest(identifier: workoutAlarmIdentifier,
content: content,
trigger: trigger)
UNUserNotificationCenter.current().add(request)
{
(error) in // ...
}
in combination with didReceive(_:withContentHandler:) you can use:
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: (daySeconds), repeats: false)
I know this isn't optimal but it should work without using deprecated classes/methods. You use repeats: false since you are intercepting the notification just before the user receives it and creating a new notification. Additionally you can use it in combination with UNNotificationAction and UNNotificationCategory if you handle multiple notifications.

This should work:
func addNotificationForAlarm(alarm: MyAlarm) {
let myAlarmNotifContent = UNMutableNotificationContent()
myAlarmNotifContent.title = "Reminder"
myAlarmNotifContent.body = alarm.activity
myAlarmNotifContent.sound = UNNotificationSound.default()
if alarm.repeatDays.count == 1 {
} else {
for index in 1...alarm.repeatDays.count {
createNotif(date: alarm.date, weekDay: index, content: myAlarmNotifContent)
}
}
}
private func createNotif(date: Date, weekDay: Int, content: UNNotificationContent) {
var dateComponent = DateComponents()
dateComponent.weekday = weekDay
dateComponent.hour = Calendar.current.dateComponents([.hour], from: date).hashValue
dateComponent.minute = Calendar.current.dateComponents([.minute], from: date).hashValue
let myAlarmTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats: true)
setupNotificationSettings()
let identifier = "Your-Notification"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: myAlarmTrigger)
let center = UNUserNotificationCenter.current()
center.add(request, withCompletionHandler: { (error) in
if error != nil {
//TODO: Handle the error
}
})
}

Related

Will this code send a notification on this day every month?

I have this code that should send a notification to the user on the same day every month. However I'm can't simulate this, so I can't test it. I was wondering if any of you could proof check it, to ensure it does send a notification every month.
Thanks
static func addNotification(){
let center = UNUserNotificationCenter.current()
let addRequest = {
let content = UNMutableNotificationContent()
content.title = "Test"
content.subtitle = "Test"
content.sound = UNNotificationSound.default
var dateComponents = DateComponents()
dateComponents.day = 31
dateComponents.hour = 12
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: "", content: content, trigger: trigger)
center.add(request)
}
center.getNotificationSettings { settings in
if settings.authorizationStatus == .authorized{
addRequest()
}
else{
center.requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
if success{
addRequest()
}
}
}
}
}

Scheduling local notifications to repeat daily from tomorrow

I'm trying to schedule a local notification to fire every day, at a specific time, but from tomorrow.
e.g. "Trigger a notification every day at 2 pm, from tomorrow"
This is how I set up my schedule function.
func scheduleNotifications(date: Date, identfier: String, after: Bool) {
let content = UNMutableNotificationContent()
content.title = "App"
content.body = "Test."
content.sound = .default
content.userInfo = ["Hour": Int(hourFormatter.string(from: date)) ?? 0]
let afterDay = Calendar.current.date(byAdding: .day, value: after ? 1 : 0, to: Date())
var components = Calendar.current.dateComponents([.hour, .minute], from: afterDay!)
components.hour = Int(hourFormatter.string(from: date)) ?? 0
components.minute = Int(minuteFormatter.string(from: date)) ?? 0
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
let request = UNNotificationRequest(identifier: identfier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
}
This code is for daily notification just call it in tomorrow schedule time
func setupNotificationSettings() {
DispatchQueue.main.async {
let content: UNMutableNotificationContent = UNMutableNotificationContent()
content.title = AppName
content.body = "APP_Body"
content.sound = UNNotificationSound.default
let trigger: UNTimeIntervalNotificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 86400, repeats: true)
let request: UNNotificationRequest = UNNotificationRequest(identifier: "\(AppName)_Local", content: content, trigger: trigger)
let center: UNUserNotificationCenter = UNUserNotificationCenter.current()
center.removeDeliveredNotifications(withIdentifiers: ["\(AppName)"])
center.removeDeliveredNotifications(withIdentifiers: ["\(AppName)"])
center.add(request) { (error) in
}
}
}

how to set local notifications between 8am and 8pm every day

So I am very new to Swift and I'm currently setting a repeating timer every 30 min after the app was launched, but i would like to only send notification between 8 am and 8 pm. Is it possible to do this without setting a reminder for each specific time?
This is how I'm currently doing this.
override func viewDidLoad(){
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.requestAuthorization(options: [.alert, .sound]) { (granted, error ) in
// enable or disable if needed.
if granted {
print("We have permission to send notifications")
} else {
print("We don't have the option to send notifications")
}
}
notificationCenter.removeAllDeliveredNotifications()
notificationCenter.removeAllPendingNotificationRequests()
// The actual notification the user will receive
let notification = UNMutableNotificationContent()
notification.title = "You should have some water"
notification.body = "It has been a long time since you had some water, why don't you have some."
notification.categoryIdentifier = "reminder"
notification.sound = .default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: (60*30), repeats: true)
let uuidString = UUID().uuidString
let request = UNNotificationRequest(identifier: uuidString, content: notification, trigger: trigger)
notificationCenter.add(request, withCompletionHandler: nil)
}
Unfortunately you do need to add a notification request for each 30 minute interval in the 8am-8pm window. What is your aversion to this approach? It's a simple for-loop. Instead of using a UNTimeIntervalNotificationTrigger you would use a UNCalendarNotificationTrigger.
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.removeAllDeliveredNotifications()
notificationCenter.removeAllPendingNotificationRequests()
let startHour = 8
let totalHours = 12
let totalHalfHours = totalHours * 2
for i in 0...totalHalfHours {
var date = DateComponents()
date.hour = startHour + i / 2
date.minute = 30 * (i % 2)
print("\(date.hour!):\(date.minute!)")
let notification = UNMutableNotificationContent()
notification.title = "You should have some water"
notification.body = "It has been a long time since you had some water, why don't you have some."
notification.categoryIdentifier = "reminder"
notification.sound = .default
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
let uuidString = UUID().uuidString
let request = UNNotificationRequest(identifier: uuidString, content: notification, trigger: trigger)
notificationCenter.add(request, withCompletionHandler: nil)
}

Why only my last local notification function is getting called?

I'm fairly new to swift and am trying to call multiple functions that request a local notification inside a UISwitch IBAction. I want to send a notification on a certain date - each quarter of the year on months 4, 7, 10, 1. Only the fourth quarter function is getting called. How can I get all four functions to be called? Here is my code:
// UISwitch for quarterly notifications
#IBAction func quarterlyFrequencyTapped(_ sender: UISwitch) {
if quarterlyFrequency.isOn == true {
firstQuarter(); secondQuarter(); thirdQuarter(); fourthQuarter()
print("quarterly frequency is \(quarterlyFrequency.isOn)")
} else {
removeQuarterlyNotification()
print("quaterly frequency is \(monthlyFrequency.isOn)")
}
}
// Functions for all four quarters
func firstQuarter() {
let firstQuarterContent = UNMutableNotificationContent()
firstQuarterContent.title = "First Quarter"
firstQuarterContent.subtitle = "Some string"
firstQuarterContent.body = "Some other string"
var firstQuarterDate = DateComponents()
firstQuarterDate.month = 3
firstQuarterDate.day = 11
firstQuarterDate.hour = 19
firstQuarterDate.minute = 20
let firstQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: firstQuarterDate, repeats: true)
let firstQuarterRequestIdentifier = "Quarterly"
let firstQuarterRequest = UNNotificationRequest(identifier: firstQuarterRequestIdentifier, content: firstQuarterContent, trigger: firstQuarterTrigger)
UNUserNotificationCenter.current().add(firstQuarterRequest, withCompletionHandler: nil)
}
func secondQuarter() {
let secondQuarterContent = UNMutableNotificationContent()
secondQuarterContent.title = "Second Quarter"
secondQuarterContent.subtitle = "Some string"
secondQuarterContent.body = "Some other string"
var secondQuarterDate = DateComponents()
secondQuarterDate.month = 3
secondQuarterDate.day = 11
secondQuarterDate.hour = 19
secondQuarterDate.minute = 21
let secondQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: secondQuarterDate, repeats: true)
let secondQuarterRequestIdentifier = "Quarterly"
let secondQuarterRequest = UNNotificationRequest(identifier: secondQuarterRequestIdentifier, content: secondQuarterContent, trigger: secondQuarterTrigger)
UNUserNotificationCenter.current().add(secondQuarterRequest, withCompletionHandler: nil)
}
func thirdQuarter() {
let thirdQuarterContent = UNMutableNotificationContent()
thirdQuarterContent.title = "Third Quarter"
thirdQuarterContent.subtitle = "Some string"
thirdQuarterContent.body = "Some other string"
var thirdQuarterDate = DateComponents()
thirdQuarterDate.month = 3
thirdQuarterDate.day = 11
thirdQuarterDate.hour = 19
thirdQuarterDate.minute = 22
let thirdQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: thirdQuarterDate, repeats: true)
let thirdQuarterRequestIdentifier = "Quarterly"
let thirdQuarterRequest = UNNotificationRequest(identifier: thirdQuarterRequestIdentifier, content: thirdQuarterContent, trigger: thirdQuarterTrigger)
UNUserNotificationCenter.current().add(thirdQuarterRequest, withCompletionHandler: nil)
}
func fourthQuarter() {
let fourthQuarterContent = UNMutableNotificationContent()
fourthQuarterContent.title = "Fourth Quarter"
fourthQuarterContent.subtitle = "Some string"
fourthQuarterContent.body = "Some other string"
var fourthQuarterDate = DateComponents()
fourthQuarterDate.month = 3
fourthQuarterDate.day = 11
fourthQuarterDate.hour = 19
fourthQuarterDate.minute = 23
let fourthQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: fourthQuarterDate, repeats: true)
//let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
let fourthQuarterRequestIdentifier = "Quarterly"
let fourthQuarterRequest = UNNotificationRequest(identifier: fourthQuarterRequestIdentifier, content: fourthQuarterContent, trigger: fourthQuarterTrigger)
UNUserNotificationCenter.current().add(fourthQuarterRequest, withCompletionHandler: nil)
}
I think you're on +iOS10 and you're using UserNotifications framework right?
Very likely your identifiers have the same and each are UPDATING the previous notification.
The reason identifier exists is to assist you with updated a previous notification that has the same identifier. For example you send a notification to update the score from 0-0 to 0-1. And instead of having 2 notifications on the screen you now have only one.
Your fix is to use different identifiers for each.
For more see this moment for the WWDC video
UPDATE after your edit:
just as I said...all your notifications have "Quarterly" as their identifier. Give them each a separate name.
Just a little help
You can just add UUID().uuidString for identifier.
Kudos Honey

Error:XPC connection interrupted

I am scheduling notification by running the loop in an array.Notification is working fine.But my application is crashing with this error.I know that the OS stops working and os gets rebooted but if i dont call this scheduleNotification function then everything works fine so i know that the error is generation because of this function when i pops to other screen and comes back again my app gets crashg but i dont know why?? please help me out.
func scheduleNotification() {
if dictInfo.object(forKey: "name") !as AnyObject as ? String == "zzz" {
} else {
let calendar = Calendar.current
var calendarComponents = DateComponents()
let strDay = (dictInfo["dd"
as NSString] !as AnyObject).doubleValue
let strMonth = (dictInfo["mm"
as NSString]) !
let dayy = strDay
let monthh = strMonth
calendarComponents.hour = 11
calendarComponents.minute = 04
calendarComponents.day = Int(dayy!) - 2
print(calendarComponents.day)
let df = DateFormatter()
df.dateFormat = "MM" // if you need 3 letter month just use "LLL"
let datee1 = df.date(from: String(describing: monthh))
print(datee1)
let monthh11 = (Calendar.current as NSCalendar).component(.month, from: datee1!)
print(monthh11) // 5
//calendarComponents.year = Int(yearr!)
calendarComponents.month = Int(monthh11)
let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: calendarComponents.month, day: calendarComponents.day, hour: calendarComponents.hour, minute: calendarComponents.minute)
if# available(iOS 10.0, * ) {
let strName: String = String(describing: dictInfo["name"] !)
let str2: String! = "Today is \(strName)\'s Birthday!"
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
let content = UNMutableNotificationContent()
content.title = "Alert Me!"
content.body = str2
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "myCategory"
content.userInfo = ((dictInfo as Any) as ? [AnyHashable: Any]) !
print(content.userInfo.count)
let request = UNNotificationRequest(identifier: strName, content: content, trigger: trigger)
// UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {
(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
} else {
}
}
}
}
MBProgressHUD.hide(
for: self.view, animated: true)
tableVieww.isHidden = false
//self.navigationItem.leftBarButtonItem?.isEnabled = true
}