一尘不染

无论启动日期如何,只要加载应用,UILocalNotification就会始终显示

swift

您好,我是Swift和IOS编程的新手。我已经设置了6条通知,这些通知应该根据一天中的时间每天提醒用户6次。警报正在运行,但是由于某种原因,当应用程序首次启动时,所有6个警报同时显示在通知中心中。任何帮助将不胜感激。

这是AppDelegate.swift中的代码

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.



        let notificationTypes : UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge
        let notificationSetting : UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(notificationSetting)

        return true
    }

这是我针对六个不同通知的功能

func prayerAlert (prayerName : String, prayHour : Int, prayMinute : Int) {


        dateComp.year = Int(currentDate.year)
        dateComp.month = Int(currentDate.month)
        dateComp.day = Int(currentDate.day)
        dateComp.hour = prayHour
        dateComp.minute = prayMinute
        dateComp.timeZone = NSTimeZone.systemTimeZone()

        var calender : NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)!
        var date : NSDate = calender.dateFromComponents(dateComp)!

        var notification : UILocalNotification = UILocalNotification()
        notification.alertBody = prayerName
        notification.fireDate = date



        UIApplication.sharedApplication().scheduleLocalNotification(notification)
    }

这就是我在ViewDidLoad中调用函数的地方

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        pray.setTimeFormat(0)


        self.locationManager.requestAlwaysAuthorization()
        self.locationManager.requestWhenInUseAuthorization()


        if CLLocationManager.locationServicesEnabled() {
            self.locationManager.delegate = self
            self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            self.locationManager.startUpdatingLocation()

        }



        var timesArray = pray.getDatePrayerTimes(currentDate.year, andMonth: currentDate.month, andDay: currentDate.day, andLatitude: locationManager.location.coordinate.latitude, andLongitude: locationManager.location.coordinate.longitude, andtimeZone: pray.timeZone)


            var convertedTime = convertPrayArray(timesArray as NSMutableArray)

            prayerAlert("Time for Fajr", prayHour: convertedTime.hourArray[0], prayMinute: convertedTime.minuteArray[0])
            prayerAlert("Time for SunRise", prayHour: convertedTime.hourArray[1], prayMinute: convertedTime.minuteArray[1])
            prayerAlert("Time for Dhuhr", prayHour: convertedTime.hourArray[2], prayMinute: convertedTime.minuteArray[2])
            prayerAlert("Time for Asr", prayHour: convertedTime.hourArray[3], prayMinute: convertedTime.minuteArray[3])
            prayerAlert("Time for Maghrib", prayHour: convertedTime.hourArray[5], prayMinute: convertedTime.minuteArray[5])
            prayerAlert("Time for Isha", prayHour: convertedTime.hourArray[6], prayMinute: convertedTime.minuteArray[6])


    }

阅读 188

收藏
2020-07-07

共1个答案

一尘不染

更新: Xcode 7.1•Swift 2.1

问题是您将日期组件设置为localTime区域而不是通知。

添加此扩展NSDate以从NSDate()返回所需的组件

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)}
    func fireDateAt(hr: Int, min: Int) -> NSDate {
        let date = NSDate()
        return NSCalendar.currentCalendar().dateWithEra(1,
            year: year,
            month: month,
            day: { hr > date.hour || (hr == date.hour && min > date.minute) ? day : day + 1 }(),
            hour: hr,
            minute: min,
            second: 0,
            nanosecond: 0
        )!
    }
}

timeZone属性

通知生效日期的时区。

fireDate中指定的日期根据该属性的值进行解释。如果指定nil(默认值),则触发日期将解释为绝对GMT时间,适用于诸如倒数计时器的情况。如果为该属性分配有效的NSTimeZone对象,则触发日期将被解释为挂钟时间,该时区会在时区发生更改时自动调整;一个适合这种情况的例子是闹钟。

func prayerAlert (prayerName : String, prayHour : Int, prayMinute : Int) {
    var notification = UILocalNotification()
    notification.timeZone  = NSTimeZone.localTimeZone()
    notification.alertBody = prayerName
    notification.fireDate  = NSDate().fireDateAt(prayHour, min: prayMinute)
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
2020-07-07