gpt4 book ai didi

swift - 如何根据 CVcalendar 中的特定时间发出每日本地通知?

转载 作者:行者123 更新时间:2023-11-30 13:12:37 24 4
gpt4 key购买 nike

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

在 View 中将出现

        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中做什么?

最佳答案

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

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

关于swift - 如何根据 CVcalendar 中的特定时间发出每日本地通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38565044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com