こんにちは、Swift と IOS プログラミングは初めてです。時間帯に応じて 1 日 6 回ユーザーに警告することになっている 6 つの通知を設定しました。アラートは機能していますが、何らかの理由でアプリを最初に起動すると、通知センターに 6 つのアラートすべてが同時に表示されます。どんな助けでも大歓迎です。
これは AppDelegate.swift のコードです
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let notificationTypes : UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge
let notificationSetting : UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSetting)
return true
}
これは、6 つの異なる通知に対する私の関数です。
func prayerAlert (prayerName : String, prayHour : Int, prayMinute : Int) {
dateComp.year = Int(currentDate.year)
dateComp.month = Int(currentDate.month)
dateComp.day = Int(currentDate.day)
dateComp.hour = prayHour
dateComp.minute = prayMinute
dateComp.timeZone = NSTimeZone.systemTimeZone()
var calender : NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)!
var date : NSDate = calender.dateFromComponents(dateComp)!
var notification : UILocalNotification = UILocalNotification()
notification.alertBody = prayerName
notification.fireDate = date
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
これは、ViewDidLoad で関数を呼び出している場所です
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pray.setTimeFormat(0)
self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
self.locationManager.startUpdatingLocation()
}
var timesArray = pray.getDatePrayerTimes(currentDate.year, andMonth: currentDate.month, andDay: currentDate.day, andLatitude: locationManager.location.coordinate.latitude, andLongitude: locationManager.location.coordinate.longitude, andtimeZone: pray.timeZone)
var convertedTime = convertPrayArray(timesArray as NSMutableArray)
prayerAlert("Time for Fajr", prayHour: convertedTime.hourArray[0], prayMinute: convertedTime.minuteArray[0])
prayerAlert("Time for SunRise", prayHour: convertedTime.hourArray[1], prayMinute: convertedTime.minuteArray[1])
prayerAlert("Time for Dhuhr", prayHour: convertedTime.hourArray[2], prayMinute: convertedTime.minuteArray[2])
prayerAlert("Time for Asr", prayHour: convertedTime.hourArray[3], prayMinute: convertedTime.minuteArray[3])
prayerAlert("Time for Maghrib", prayHour: convertedTime.hourArray[5], prayMinute: convertedTime.minuteArray[5])
prayerAlert("Time for Isha", prayHour: convertedTime.hourArray[6], prayMinute: convertedTime.minuteArray[6])
}