通知不会在一周的特定日期弹出

时间:2018-03-07 13:14:21

标签: ios swift uilocalnotification

我正在开发一个警报应用程序,用户可以在其中选择他想要响铃的星期几。在我的代码中,如果我执行"同一天的条件"只有每天警报响起。但是,当我选择其他两个条件时,即"第二天"和"前一天",通知未触发。虽然通知已创建,但它们未触发。任何帮助,将不胜感激。这是我的代码

func scheduleUserNotifications(for alarm: Alarm)
    {
        let notificationContent = UNMutableNotificationContent()
        notificationContent.title = "HelloK"
        if alarm.name.isEmpty == false
        {
            notificationContent.body = "Please respond to your alarm \(alarm.name)"
        }
        else
        {
            alarm.name = "Empty" 
        }
        let song = AlarmController.shared.tone
        print (song)
        notificationContent.sound = UNNotificationSound(named: song)
        print(notificationContent.sound)
        print(AlarmController.shared.counter)
        var countNum: Int!
        if AlarmController.shared.counter == "30 Minutes"
        {
            countNum = 6
        }
        else if AlarmController.shared.counter == "1 Hour"
        {
            countNum = 12
        }
        else
        {
            countNum = 36
        }

        print(countNum)

        var daysValue: [Int]
        daysValue = AlarmController.shared.days

        print(daysValue)
        var dateVal = 0
        for day in daysValue
        {
            print(dateVal)
            if day == 1
            {
                print("hello")

                guard let fireDate = alarm.fireDate else { return }
                print(fireDate)

                let dateToChange = fireDate
                var nextDate = dateToChange
                var difference : Int?
//                var addComponent : Calendar.Component?
                let weekday = Calendar.current.component(.weekday, from: fireDate)
                print(weekday)

                if dateVal == weekday-1
                {
                    print("same day")
                    nextDate = fireDate
//                  nextDate = fireDate.addingTimeInterval(7*86400)
                    print(nextDate)
                }
                else if dateVal > weekday-1
                {
                    print("next day")
                    difference = dateVal - (weekday-1)
                    print(difference!)
                    nextDate = fireDate.addingTimeInterval(TimeInterval(difference!*86400))
                    print(nextDate)
                }
                else if dateVal < weekday-1
                {
                    print("previous day")
                    difference = 7-(weekday-1)+dateVal
                    print(difference!)
                    nextDate = fireDate.addingTimeInterval(TimeInterval(difference!*86400))
                    print(nextDate)
                }

                for ind in 0...countNum
                {
                    let date = nextDate.addingTimeInterval((1.0*Double(ind)) * 60.0)
                    print(date)

                    let triggerDate = Calendar.current.dateComponents([.weekday, .hour, .minute, .second], from: date)
                    DispatchQueue.main.async
                        {
                            print(triggerDate)
                            let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: true)
                            let request = UNNotificationRequest(identifier: "\(alarm.uuid)\(ind)", content: notificationContent, trigger: trigger)
//                            print(request)
                            UNUserNotificationCenter.current().add(request)
                            {
                                (error) in
                                if let error = error
                                {
                                    print("Unable to add notification request, \(error.localizedDescription)")
                                }
                            }

                        }
                }

            }

            dateVal += 1
        }
        let center = UNUserNotificationCenter.current()
        center.getPendingNotificationRequests { (notifications) in
            print("Count: \(notifications.count)")
            for item in notifications {
                print(item.content)
            }
        }

    }

当我删除我的代码的这部分时,通知工作正常,但它们每天都会触发

else if dateVal > weekday-1
                {
                    print("next day")
                    difference = dateVal - (weekday-1)
                    print(difference!)
                    nextDate = fireDate.addingTimeInterval(TimeInterval(difference!*86400))
                    print(nextDate)
                }
                else if dateVal < weekday-1
                {
                    print("previous day")
                    difference = 7-(weekday-1)+dateVal
                    print(difference!)
                    nextDate = fireDate.addingTimeInterval(TimeInterval(difference!*86400))
                    print(nextDate)
                }

但是当我取消注释时,不会显示通知

0 个答案:

没有答案