gpt4 book ai didi

Swift 多个本地通知仅触发第一个元素

转载 作者:行者123 更新时间:2023-11-30 13:31:36 25 4
gpt4 key购买 nike

我对本地通知有疑问,我在 stackover 中看到了很多答案,但没有找到我需要的内容。

我有这个 Json 数据

ModulesRegistered =         (
{
ModuleCode = "COMP 0304.1";
ModuleName = "Object Oriented Paradigm";
Session = "Session-C ";
Timetable = (
{
Day = Sunday;
EndTime = "18.00";
FacultyName = "Renjini Jose";
RoomNo = "IBR 112";
StartTime = "16.00";
}
);
},
{
ModuleCode = "COMP 0311";
ModuleName = "Operating Systems";
Session = "Session-D ";
Timetable = (
{
Day = Wednesday;
EndTime = "20.00";
FacultyName = "Sheela Paudyal";
RoomNo = "IBK 102";
StartTime = "17.00";
}
);
},
{
ModuleCode = "COMP 10013";
ModuleName = "User Interface Design";
Session = "Session-B ";
Timetable = (
{
Day = Thursday;
EndTime = "18.00";
FacultyName = "Mohammed Kaleem";
RoomNo = "AKZ 205";
StartTime = "16.00";
},
{
Day = Wednesday;
EndTime = "17.00";
FacultyName = "Mohammed Kaleem";
RoomNo = "IBK 009";
StartTime = "15.00";
}
);
},
{
ModuleCode = "PROJ 0001";
ModuleName = "PROJECT I";
Session = "Session-U ";
Timetable = (
{
Day = Tuesday;
EndTime = "20.00";
FacultyName = "Kamal Uddin Sarker";
RoomNo = "AKZ 121";
StartTime = "18.00";
}
);
}
);

我想为数据的每个元素创建本地通知

Swift 代码

Controller 代码:

override func viewDidLoad() {
super.viewDidLoad()

let noti = Notification()
noti.deleteAll()

// Add notification

if let GetData = DataService.DataServiceInstance.LoadData[0]["ModulesRegistered"] as? NSArray{


for ModulesRegistered in GetData {



if let TimeTable = ModulesRegistered["Timetable"] as? NSArray{

for TimeTableData in TimeTable {


if let TimeTableDic = TimeTableData as? Dictionary<String,String> {

if let moduleName = ModulesRegistered["ModuleName"] as? String{
noti.createNotification(TimeTableDic["StartTime"]!, day: TimeTableDic["Day"]!, msgBody: "Your \(moduleName) will begin after one hour from now , your classroom address is \(TimeTableDic["RoomNo"]!)")

}




}

}


}

}

}

通知类 notification.swift 创建通知函数

func createNotification(hour:String, day:String, msgBody:String)->Bool
{

print(day)
let TheTime:String = ("\(day) \(hour) UTC+04:00")
let fireDate = TheTime.stringByReplacingOccurrencesOfString(".", withString: ":", options: NSStringCompareOptions.LiteralSearch, range: nil)


let dateFormatter = NSDateFormatter()
//var dateAsString = "Thursday 20:27 UTC+04:00"
dateFormatter.dateFormat = "EEEE HH:mm ZZZZZ"

dateFormatter.locale = NSLocale(localeIdentifier: "en_OM")
let newDate = dateFormatter.dateFromString(fireDate)
print(newDate)




let notificationClass:UILocalNotification = UILocalNotification()
notificationClass.alertBody = msgBody
notificationClass.fireDate = newDate
notificationClass.soundName = UILocalNotificationDefaultSoundName
notificationClass.repeatCalendar = NSCalendar.currentCalendar()
notificationClass.repeatInterval = NSCalendarUnit.Day
notificationClass.timeZone = NSTimeZone(name: "UTC")
notificationClass.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
UIApplication.sharedApplication().scheduleLocalNotification(notificationClass)



return true
}

代码正在运行,但所有通知每天都会触发,我不希望这样,我希望每个通知都应该在他的一天触发。

谢谢

最佳答案

您的问题是由这一行引起的:

notificationClass.repeatInterval = NSCalendarUnit.Day

设置 UILocalnotification 的重复间隔。默认值为 0,表示系统不会重新安排通知。

https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/#//apple_ref/occ/instp/UILocalNotification/repeatInterval

删除该行就可以了。

关于Swift 多个本地通知仅触发第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36515050/

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