로컬 알림을위한 공용 함수가 있으며이 함수를 applicationWillTerminate에서 호출하려고합니다. 내가 이것을 시도하면 아무 일도 일어나지 않습니다. viewDidLoad에서 호출 할 때 올바르게 작동하기 때문에 지역 알림 기능이 정상적으로 작동합니다.Swift - applicationWillTerminate에서 로컬 알림 메서드 호출
이것은 로컬 알림 기능입니다.
func scheduleLocalNotification(second: Double) {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.badge = 1
content.title = "Title!"
content.body = "Message!"
content.categoryIdentifier = "id"
content.userInfo = ["key": "value"]
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: second, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request)
} else {
// Fallback on earlier versions
}
}
AppDelegate;
나는 비슷한 문제를 찾아 SO를 찾았지만 두 가지 질문이 있지만 그 중 아무 것도 내 문제를 해결하지 못했습니다.
Send local notification after termination app swift 2
Local notification on application termination
편집 : 요청 권한 부여;
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore")
if (!launchedBefore) {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted {
print("Authorization granted!")
} else {
print("Authorization not granted!")
}
}
} else {
let settings = UIUserNotificationSettings(types: [.alert, .badge , .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
}
UserDefaults.standard.set(true, forKey: "launchedBefore")
}
// Override point for customization after application launch.
return true
}
어떻게 당신이 테스트 응용 프로그램을 종료하고 있습니다 : 당신은 아이폰 OS의 동작을 테스트하는 경우
, 당신은 코너 가장자리에서, 여기에 "에스키모"대답을 볼 수 있습니까? – Paulw11
@ Paulw11이 버튼을 두 번 누르고 앱을 제거합니다. 나는 또한 테스트를 위해 실제 장치를 사용하고 있습니다. – THO
코드의 어딘가 이전에 requestAuthorization (options : completionHandler :)를 했습니까? – rmp