How To Use UILocalNotification In Swift

前端 未结 6 2072
温柔的废话
温柔的废话 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: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.

提交回复
热议问题