2017-02-14 1 views
1

로컬 알림을위한 공용 함수가 있으며이 함수를 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 
    } 
+0

어떻게 당신이 테스트 응용 프로그램을 종료하고 있습니다 : 당신은 아이폰 OS의 동작을 테스트하는 경우

, 당신은 코너 가장자리에서, 여기에 "에스키모"대답을 볼 수 있습니까? – Paulw11

+0

@ Paulw11이 버튼을 두 번 누르고 앱을 제거합니다. 나는 또한 테스트를 위해 실제 장치를 사용하고 있습니다. – THO

+0

코드의 어딘가 이전에 requestAuthorization (options : completionHandler :)를 했습니까? – rmp

답변

0

당신이 연결된 두 번째 질문에 대한 답변은 관련 보인다 - applicationWillTerminate는 시스템이 백그라운드에서있는 동안 당신의 응용 프로그램을 종료하기로 결정하면 전화를받을 보장 할 수 없습니다.

+0

평소와 같이 포 그라운드에있는 동안 앱을 종료합니다. – THO

0

응용 프로그램에 대한 메모입니다 .WillTerminate : Apple 주 및 "Uncommon"에서 말하는대로 보장 할 수 없습니다.

그러나 내 생각은 설계 문제 :

a) 귀하가 로컬 알림을 추가 할 경우, 즉시 사용자가 설정 한대로 해.

b) "applicationWillTerminate"를 통해 전달할 때 CPU 시간을 절약하기 위해 종료 할 때만 "추가 알림"을 ​​호출하면 매번 큰 벌칙 저장 알림이 없어야합니다. 표준 행동으로, iOS에서 우리는 사용자가 그것을 설정하자마자 모든 것을 저장해야합니다.

내 앱 내 간단히 "뒤로"또는 "닫기"에 대한 알림을 추가합니다.

c) 이미 설정된 알림을 제어/재설정해야합니다.

https://forums.developer.apple.com/thread/13557