Swift Multiple Local Notification仅限第一个emelent

时间:2016-04-09 09:34:48

标签: swift ios9

我有本地通知的问题,我在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 Code

控制器代码:

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
}

代码正在运行,但所有通知每天都会被解雇,我不希望这样,我希望每个人都应该在他那天开火。

谢谢

1 个答案:

答案 0 :(得分:0)

您的问题是由此行引起的:

notificationClass.repeatInterval = NSCalendarUnit.Day

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

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

删除该行,你会没事的。