如何根据CVcalendar中的具体时间进行每日本地通知?

时间:2016-07-25 10:14:51

标签: swift localnotification

enter image description here我正在使用cvcalendar,并且每天都有不同的时间用于fajer,dohor,aser,maghreb,ishaa。例如,我选择了Adan for Fajer,所以我希望每天都能获得adan,而且每天都有不同的时间。因此,当我在DidreceivedLocalNotification中收到通知时,我希望在第二天的日历中获取并获取第二天的时间,因为我知道从CoreData获取时间。

在viewWillappear中

        let date = NSDate()
        let dateFormatter = NSDateFormatter()

        dateFormatter.dateFormat = "dd-MM-yyyy"
        let calendar = NSCalendar.currentCalendar()
        let calendarForDate = NSCalendar.currentCalendar()
        let componentsForDate = calendar.components([.Day , .Month , .Year], fromDate: date)

        let year =  componentsForDate.year
        let month = componentsForDate.month
        let day = componentsForDate.day
        //////////
        //// Conditions after selecting the user (vibration, beep, Adan ) these conditions are testing the selected choice to send a notification to the user on his choice
        //



        if prayerCommingFromAdan.id == 0 && prayerCommingFromAdan.ringToneId != 0{

            notificationId.id = 0
            let hours = prayer0.time[0...1]
            let minutes = prayer0.time[3...4]
            let fajerTime = "\(month)-\(day)-\(year) \(hours):\(minutes)"
            var dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "MM-dd-yyyy hh:mm"
            dateFormatter.timeZone = NSTimeZone.localTimeZone()

            // convert string into date
            let dateValue = dateFormatter.dateFromString(fajerTime) as NSDate!





            var dateComparisionResult:NSComparisonResult = NSDate().compare(dateValue)

            if dateComparisionResult == NSComparisonResult.OrderedAscending
            {
                addNotificationAlarm(year, month: month, day: day, hour: prayer0.time[0...1], minutes: prayer0.time[3...4], soundId: prayerCommingFromAdan.ringToneId, notificationBody: "It is al fajr adan")

            }

我应该怎么做AppDelegate中的DidreceivedLocalNotification?

1 个答案:

答案 0 :(得分:0)

您可以使用此代码根据时间安排每日通知。

 func ScheduleMorning() {
        let calendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
        var dateFire=NSDate()

        var fireComponents = calendar.components([.Hour, .Minute, .Second], fromDate: dateFire)


        if (fireComponents.hour >= 7) {
            dateFire=dateFire.dateByAddingTimeInterval(86400)  // Use tomorrow's date

            fireComponents = calendar.components([.Hour, .Minute, .Second], fromDate: dateFire)

        }


        fireComponents.hour = 7
        fireComponents.minute = 0
        fireComponents.second = 0

        // Here is the fire time you can change as per your requirement
        dateFire = calendar.dateFromComponents(fireComponents)!

        let localNotification = UILocalNotification()
        localNotification.fireDate = dateFire // Pass your Date here
        localNotification.alertBody = "Your Message here."
        localNotification.userInfo = ["CustomField1": "w00t"]
        localNotification.repeatInterval = NSCalendarUnit.Day

        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)    }

接收

 func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
        // Do something serious in a real app.
        print("Received Local Notification:")
        print(notification.userInfo)
    }