알람 응용 프로그램 작업 중, 특정 시간에 알람을 예약해야하는데 알람 예약을 위해 scheduleLocalNotification
을 사용합니다. 내가 원하는대로. BUT 알람을 시작하기 전에 API 서버에 대한 요청을 실행해야합니다. 해당 요청에서 일부 조건을 충족하는 경우 API 서버에서 반환하는 일부 매개 변수를 확인하고 싶습니다. 하나는 특정 날짜에 실행하는 방법이있는 경우백그라운드에서 특정 날짜 - 시간에 신속하게 작업을 실행하는 방법 응용 프로그램이 켜져 있거나 꺼져 있습니다.
- 시간을 신속 에를 로컬 통지를 통해 응용 프로그램을 깨워서 그
func addAlarm (newAlarm: Alarm) {
// Create persistent dictionary of data
var alarmDictionary = NSUserDefaults.standardUserDefaults().dictionaryForKey(ALARMS_KEY) ?? Dictionary()
// Copy alarm object into persistent data
alarmDictionary[newAlarm.UUID] = newAlarm.toDictionary()
// Save or overwrite data
NSUserDefaults.standardUserDefaults().setObject(alarmDictionary, forKey: ALARMS_KEY)
scheduleNotification(newAlarm, category: "ALARM_CATEGORY")
scheduleNotification(newAlarm, category: "FOLLOWUP_CATEGORY")
}
/* NOTIFICATION FUNCTIONS */
func scheduleNotification (alarm: Alarm, category: String) {
let notification = UILocalNotification()
notification.category = category
notification.repeatInterval = NSCalendarUnit.Day
switch category {
case "ALARM_CATEGORY":
notification.userInfo = ["UUID": alarm.UUID]
notification.alertBody = "Time to wake up!"
notification.fireDate = alarm.wakeup
notification.timeZone = NSTimeZone.localTimeZone()
notification.soundName = "loud_alarm.caf"
break
case "FOLLOWUP_CATEGORY":
notification.userInfo = ["UUID": alarm.followupID]
notification.alertBody = "Did you arrive yet?"
notification.fireDate = alarm.arrival
notification.timeZone = NSTimeZone.localTimeZone()
notification.soundName = UILocalNotificationDefaultSoundName
break
default:
print("ERROR SCHEDULING NOTIFICATION")
return
}
print("Notification=\(notification)")
// For debugging purposes
if alarm.isActive {
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
}
불행히도 로컬 알림은 자동 알림 일 수 없으며 원격 알림 만 가능합니다. 내 충고는 서버에 알림 기능을 구현하여 사용자가 알람을 원하는 시간에 서버에 알려주고 서버가 그 시간에 메서드를 실행하고 원하는 검사를 수행 한 다음 통과하면 원격 알람을 트리거하는 장치에 알림. –
@ JacobKing 귀하의 조언을 주셔서 감사합니다 잠시만 기다려 보겠습니다. 사실 주어진 시간대에 백그라운드에서 실행되는 기능이 필요합니다. 어떤 사람들은 내가 잠시 동안 기다릴 것이라고 생각합니다. 만약 내가 어떤 유능한 대답을 얻지 못하면 나는 그것을 서버에 구현할 것이다. –