Swift Notification Fire from Datepicker

Swift notification fire from datePicker

Try like this:

class ViewController: UIViewController {

@IBOutlet var datePicker: UIDatePicker!
@IBOutlet var notificationSwitch: UISwitch!

let localNotification = UILocalNotification()

override func viewDidLoad() {
super.viewDidLoad()
setUpNotificationsOptions()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

}

func setUpNotificationsOptions() {
datePicker.datePickerMode = .Time
localNotification.timeZone = NSTimeZone.localTimeZone()
localNotification.repeatInterval = .Day
localNotification.alertAction = "Open App"
localNotification.alertBody = "a notification"
localNotification.soundName = UILocalNotificationDefaultSoundName
}

func toggleNotification() {
if notificationSwitch.on {
localNotification.fireDate = datePicker.date.fireDate
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
} else {
localNotification.fireDate = nil
UIApplication.sharedApplication().cancelLocalNotification(localNotification)
}
}
@IBAction func toggleSwitch(sender: UISwitch) {
toggleNotification()
}
@IBAction func dateChanged(sender: UIDatePicker) {
toggleNotification()
}
}

you will need those extensions:

extension NSDate {
var minute: Int {
return NSCalendar.currentCalendar().component(.Minute, fromDate: self)
}
var hour: Int {
return NSCalendar.currentCalendar().component(.Hour, fromDate: self)
}
var day: Int {
return NSCalendar.currentCalendar().component(.Day, fromDate: self)
}
var month: Int {
return NSCalendar.currentCalendar().component(.Month, fromDate: self)
}
var year: Int {
return NSCalendar.currentCalendar().component(.Year, fromDate: self)
}
var fireDate: NSDate {
let today = NSDate()
return NSCalendar.currentCalendar().dateWithEra(1,
year: today.year,
month: today.month,
day: { hour > today.hour || (hour == today.hour
&& minute > today.minute) ? today.day : today.day+1 }(),
hour: hour,
minute: minute,
second: 0,
nanosecond: 0
)!
}
}

Scheduling weekly repeatable local notification with fire date from date picker in Swift 3

As McNight mentionned, you can use UNCalendarNotificationTrigger like so:

let interval = 60 * 60 * 24 * 7 - 300 // One week minus 5 minutes.
let alarmTime = Calendar.current.date(byAdding: .second, value: interval, to: Date())!
let components = Calendar.current.dateComponents([.weekday, .hour, .minute], from: alarmTime)
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)

(I haven't tested this code, but this should get you on the right path).

More information here.

Edit: Fixed time interval calculation suggested by SwiftyCruz.

Edit 2: Updated to use a Calendar to perform the time shift as suggested by RickiG.

Local UNMutableNotificationContent fire date using Date picker

You need to provide the time interval between 'now' and when you want the notification to be sent.

let interval = laterDate.timeIntervalSince(earlierDate)

In this case, earlierDate should be 'now' (ie. just Date()), and I assume laterDate is your datePicker.date.

local notification not working at the exact time as selected from datepicker but after a few seconds in swift 3

I found this answer that says UILocalNotification fire time isn't that accurate. I was also going through Apple's documentation on Timer and found this regarding timer tolerance

The system reserves the right to apply a small amount of tolerance to
certain timers regardless of the value of the tolerance property.

Since the scheduled notification will be using a timer behind the scenes, the system must be applying a small tolerance to improve power saving. If you want to read more about timer tolerance read this Apple's documentation on timer.

User set time for notification in swift

//
// ViewController.swift
// Combining Date and Time
//
// Created by Leonardo Savio Dabus on 08/12/2014.
// Copyright (c) 2014 inDabusiness.com. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

// IBOutlet goes here
@IBOutlet var myDatePicker: UIDatePicker!
@IBOutlet var mySwitch: UISwitch!

// let = whatever goes here
// var = whatever goes here
var localNotification = UILocalNotification() // You just need one
var notificationsCounter = 0

// put your functions now
func datePicker() { myDatePicker.datePickerMode = UIDatePickerMode.Date }
func datePickerDefaultDate() { myDatePicker.date = NSDate().xDays(+1) }
func notificationsOptions() {
localNotification.timeZone = NSTimeZone.localTimeZone()
localNotification.repeatInterval = .CalendarUnitDay
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
localNotification.alertAction = "Open App"
localNotification.alertBody = "Here is the seven o'clock notification"
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
// you may add arbitrary key-value pairs to this dictionary.
// However, the keys and values must be valid property-list types
// if any are not, an exception is raised.
// localNotification.userInfo = [NSObject : AnyObject]?
}
func toggleSwitch(){
if mySwitch.on{
localNotification.fireDate = myDatePicker.date.fireDate // combined date = picked Date + 7:00am time
} else {
localNotification.fireDate = NSDate(timeIntervalSinceNow: 999999999999) // will never be fired
}
}
override func viewDidLoad() {
super.viewDidLoad()
datePicker()
datePickerDefaultDate()
notificationsOptions()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// here is where you place your IBActions

@IBAction func switchPressed(sender: AnyObject) {
toggleSwitch()

}
}

create a new Swift Source file at your project to put your extensions

import Foundation
public extension NSDate {
func xDays(x:Int) -> NSDate {
return NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay, value: x, toDate: self, options: nil)!
}
var day: Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitDay, fromDate: self).day }
var month: Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitMonth, fromDate: self).month }
var year: Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitYear, fromDate: self).year }
var fireDate: NSDate { return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: 7, minute: 0, second: 0, nanosecond: 0)! }
}


Related Topics



Leave a reply



Submit