【问题标题】:how to make daily local notification depending on specific time in CVcalendar?如何根据 CVcalendar 中的特定时间进行每日本地通知?
【发布时间】:2016-07-25 10:14:51
【问题描述】:

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

在视图中会出现

        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应该怎么做?

【问题讨论】:

    标签: swift localnotification


    【解决方案1】:

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

     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)
        }
    

    【讨论】:

    • Anand 谢谢,但我的问题是如何在日历中获取第二天的时间以及如何在 AppDelegate 中使用 DidreceivedLocalNotification ?
    • 我在答案中添加了接收代码。请检查一下。
    • 您可以使用 NSDate 手动设置时间来设置本地通知。
    • Anand ,日历中每天有 5 个不同的时间,我们使用 API 来获取这些时间一年。所以不能手动设置它们。我只需要如何访问第二天的时间以每天设置自动通知。
    • 您可以根据需要更改此代码,我在这里为您提供如何做这件事的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    相关资源
    最近更新 更多