How To Use UILocalNotification In Swift

前端 未结 6 1922
温柔的废话
温柔的废话 2021-02-06 01:59

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         


        
相关标签:
6条回答
  • 2021-02-06 02:09

    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.

    0 讨论(0)
  • 2021-02-06 02:16

    In Swift, to cancel the particular local notification using Unique Key:

    func cancelLocalNotification(UNIQUE_ID: String){
    
            var notifyCancel = UILocalNotification()
            var notifyArray = UIApplication.sharedApplication().scheduledLocalNotifications
    
            for notifyCancel in notifyArray as! [UILocalNotification]{
    
                let info: NSDictionary = notifyCancel.userInfo as! [String : String]
    
                if info[UNIQUE_ID]!.isEqual(UNIQUE_ID){
    
                    UIApplication.sharedApplication().cancelLocalNotification(notifyCancel)
                }else{
    
                    println("No Local Notification Found!")
                }
            }
        }
    
    0 讨论(0)
  • 2021-02-06 02:21

    Would be nice to also separate out some of the components:

    private let kLocalNotificationMessage:String = "Your message goes here!"
    private let kLocalNotificationTimeInterval:NSTimeInterval = 5
    
    private func LocalNotification() -> UILocalNotification {
      var localNotification:UILocalNotification = UILocalNotification()
      localNotification.fireDate = NSDate(timeIntervalSinceNow:kLocalNotificationTimeInterval)
      localNotification.alertBody = kLocalNotificationMessage
      return localNotification
    }
    
    private func ScheduleLocalNotificationIfPossible() {
      if (UIApplication.sharedApplication().isRegisteredForRemoteNotifications()) {
        UIApplication.sharedApplication().scheduleLocalNotification(LocalNotification())
      }
    }
    

    Now you can call, ScheduleLocalNotificationIfPossible() to schedule the local notification if the user has registered for remote notifications.

    0 讨论(0)
  • 2021-02-06 02:22

    There's also support for creating the date like so:

    NSDate(timeIntervalSinceNow: 15)
    
    0 讨论(0)
  • 2021-02-06 02:23
    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)
    }
    
    0 讨论(0)
  • 2021-02-06 02:31

    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"
    
    0 讨论(0)
提交回复
热议问题