繁体   English   中英

如何在Swift中使用本地通知?

[英]How to use local notifications in Swift?

我正在尝试创建一个每天23:59:59触发的通知,这是我到目前为止所做的-

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert |
    UIUserNotificationType.Badge, categories: nil))

let calendar = NSCalendar(identifier: NSGregorianCalendar)

let cal = NSCalendar.currentCalendar()
let datefor = NSDate()
let comp = cal.components(.MonthCalendarUnit | .DayCalendarUnit | .YearCalendarUnit, fromDate: datefor)

let components = NSDateComponents()
components.year = comp.year
components.month = comp.month
components.day = comp.day
components.hour = 23
components.minute = 59
components.second = 59

let date = calendar?.dateFromComponents(components)

var localNotification:UILocalNotification = UILocalNotification()
localNotification.alertAction = "Checking"
localNotification.alertBody = "Hello"
localNotification.fireDate = date
localNotification.timeZone = NSTimeZone.localTimeZone()
localNotification.repeatInterval = NSCalendarUnit.DayCalendarUnit
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)    

该代码不起作用,我仅收到一个通知,仅此而已。 代码中有什么问题?

您是否尝试过在Iphone上进行测试? 以下是我正在从事的项目的代码。 (请注意,仅在应用程序首次运行时才调用此段代码,如果您继续运行该应用程序,则会添加更多时间表)

  /*notification process */
    //setting up date
    //year
    var year = Int( NSCalendar.currentCalendar().component(NSCalendarUnit.CalendarUnitYear, fromDate: NSDate(timeIntervalSinceNow: 0) ) )

     //month
      var month = Int( NSCalendar.currentCalendar().component(NSCalendarUnit.CalendarUnitMonth, fromDate: NSDate(timeIntervalSinceNow: 0) ) )

     //day
     var day = Int( NSCalendar.currentCalendar().component(NSCalendarUnit.CalendarUnitDay, fromDate: NSDate(timeIntervalSinceNow: 0) ) )

    println( "Date: \(month)|\(day)|\(year)" )//testing


     //hour
     var hour = Int( NSCalendar.currentCalendar().component(NSCalendarUnit.CalendarUnitHour, fromDate: NSDate(timeIntervalSinceNow: 0) ) )

     //minute
     var minute = Int( NSCalendar.currentCalendar().component(NSCalendarUnit.CalendarUnitMinute, fromDate: NSDate(timeIntervalSinceNow: 0) ) )

    //second
    var second = Int( NSCalendar.currentCalendar().component(NSCalendarUnit.CalendarUnitSecond, fromDate: NSDate(timeIntervalSinceNow: 0) ) )

    println( "Time: \(hour):\(minute):\(second)" )//testing

    //time is passed the desired schedule time
    if(hour > 8){
      day++

       //process the month of february
       if(month == 2){
           //determining if it is leap year
           var leapyear = self.isLeapYear(year)

           //leap year & feb days have exhausted
           if(leapyear){
              if(day > 29){
                 day = 1
                 month++
              }
           }
            //not leap year & feb days have exhausted
            else if(day > 28 ){
                day = 1
                month++
             }
            }
            else if(day > 30){
               //months with 30 max 30 days ( April, June, Semptember, November)
               if( (month == 4) || (month == 6) || (month == 9) || (month == 11){
                   day = 1
                   month++
               }
               else{
                  //months with 31 days
                  if(day > 31){
                      day = 1
                      month++

                      //reached the end of the year
                       if(month > 12){
                            month = 1
                            year++
                       }
                     }
                    }
                   }
                  }

                  println("updated date: \(month)|\(day)|\(year)")
                  println("updated time: \(hour)|\(minute)|\(second)")

                  //customize a specific start date and time
                  var dateComp:NSDateComponents = NSDateComponents()
                  dateComp.year   = year
                  dateComp.month  = month
                  dateComp.day    = day
                  dateComp.hour   = Int(8)
                  dateComp.minute = Int(0)
                  dateComp.second = Int(0)
                  dateComp.timeZone = NSTimeZone.systemTimeZone()

                  var calendar = NSCalendar.currentCalendar()
                  var date:NSDate = calendar.dateFromComponents(dateComp)!

                  println(calendar.description.debugDescription)
                  println(dateComp.debugDescription)
                  println(date.debugDescription)

                  //notification settings
                  var localNotification: UILocalNotification = UILocalNotification()
                   localNotification.alertAction = "Daily notification"
                   localNotification.alertBody = "Message goes here"
                   localNotification.timeZone = NSTimeZone.systemTimeZone()
                   localNotification.fireDate = date

                    //repeat settings
                    localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay

                    localNotification.repeatCalendar = NSCalendar.currentCalendar()

                    localNotification.category = "INVITE_CATEGORY";  //needed for multiple request in app delegate

                    println("- - - - - - - - - - - - - - - - - - - -")
                    println("schedule set for: \(localNotification.debugDescription)")

                                   UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM