Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Use UILocalNotification In Swift

I am trying to figure out how to setup a UILocalNotification in swift but I am not having a lot of luck. I am trying this:

var notification = UILocalNotification()
notification.timeZone = NSTimeZone.defaultTimeZone()
var dateTime = NSDate.date()
notification.fireDate(dateTime)
notification.alertBody("Test")
UIApplication.sharedApplication().scheduleLocalNotification(notification)

For starters, I am not sure if this is the proper way to get the current date time. In .Net, I would just do DateTime.Now().

Second, when I try this, I get an error that says:

'(@lvalue NSDate!) -> $T3' is not identical to 'NSDate'

Unfortunately I have no idea what this means or how to proceed.

like image 618
PretzelJesus Avatar asked Jul 01 '14 22:07

PretzelJesus


3 Answers

First, you construct an NSDate using initializer syntax:

let dateTime = NSDate()

The documentation shows how ObjC convenience constructors map to Swift initializers. If the docs show an init() for a class, you call it using the name of the class: for NSDate, init() means you call NSDate(), init(timeInterval:sinceDate:) means you call NSDate(timeInterval: x, sinceDate: y), etc.

Second: fireDate isn't a method, it's a property. You should assign to it instead of trying to call it:

notification.fireDate = dateTime

Ditto for alertBody.

You can also find the Swift syntax for Cocoa APIs by command-clicking a class name (or other API symbol) in your Swift source file; this causes Xcode to generate a "Swift-ified" version of the relevant header file.

like image 94
rickster Avatar answered Oct 19 '22 11:10

rickster


func setupNotificationReminder() {
    var title:String = "Your reminder text goes here"

    let calendar = NSCalendar.currentCalendar()
    let calendarComponents = NSDateComponents()
    calendarComponents.hour = 7
    calendarComponents.second = 0
    calendarComponents.minute = 0
    calendar.timeZone = NSTimeZone.defaultTimeZone()
    var dateToFire = calendar.dateFromComponents(calendarComponents)

    // create a corresponding local notification
    let notification = UILocalNotification()

    let dict:NSDictionary = ["ID" : "your ID goes here"]
    notification.userInfo = dict as! [String : String]
    notification.alertBody = "\(title)"
    notification.alertAction = "Open"
    notification.fireDate = dateToFire
    notification.repeatInterval = .Day  // Can be used to repeat the notification
    notification.soundName = UILocalNotificationDefaultSoundName
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
like image 32
bwash70 Avatar answered Oct 19 '22 13:10

bwash70


Not answering your question but worth the note:

notification.fireDate(dateTime)
notification.alertBody("Test")

will also throw a compiler error saying that it can't find the init. do this instead

notification.fireDate = NSDate(timeIntervalSinceNow: 15)
notification.alertBody = "Notification Received"
like image 26
arcognito Avatar answered Oct 19 '22 12:10

arcognito