0

앱을 처음 실행할 때 시뮬레이터는 앱이 포 그라운드에있을 때 알림을 표시 할 수 있습니다 .`willPresent notification`이 호출되지 않고 iOS 10에서 앱이 포 그라운드에있을 때 로컬 알림이 표시되지 않음

willPresent notification 대신 앱 didReceive notification: (즉, 알림 방법)을 다시 실행하면 앱이 포 그라운드에있는 동안 알림이 표시되지 않습니다. 그러나 앱이 백그라운드에있는 경우 알림이 표시됩니다.

question 또는 question이이 문제에 대한 해결책을 제공하지 않습니다.

나는이 코드를 사용하여 알림 승인을 얻을 :

func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]? = [:]) -> Bool { 
       if #available(iOS 10.0, *) { 
        let center = UNUserNotificationCenter.current() 
        center.requestAuthorization(options: [.alert, .sound, .sound]) { (granted, error) in    
        UNUserNotificationCenter.current().delegate = self     
        }      
       } 
       return true 
      } 

을 그리고 나는 willPresent 통지 방법을 구현했습니다 :

let content = UNMutableNotificationContent() 
     content.title = title 
     content.subtitle = subtitle 
     content.body = message 
     content.categoryIdentifier = identifier      

     let trigger = UNTimeIntervalNotificationTrigger(
      timeInterval: 10.0, 
      repeats: false) 

     let request = UNNotificationRequest(
      identifier: "identifier", 
      content: content, 
      trigger: trigger 
     ) 

     UNUserNotificationCenter.current().add(
      request, withCompletionHandler: nil) 
:

여기
@available(iOS 10.0, *) 
      func userNotificationCenter(_ center: UNUserNotificationCenter, 
             willPresent notification: UNNotification, 
             withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)      completionHandler(UNNotificationPresentationOptions.alert) 
      } 

내가 통지를 만드는 방법을

간단히 말해서 문제 : 앱이 처음 실행 된 후 앱이 포 그라운드에있을 때 앱이 알림을 표시 할 수 있도록하려면 어떻게해야합니까?

미리 감사드립니다.

+0

모든 알림에 대해 식별자가 동일하기 때문입니다. 다른 알림에 대해 다른 식별자를 추가해야합니다. 또한, 왜 .sound를 허가로 두 번 요청 했습니까? – bhakti123

답변

1

requestAuthorization 완료 블록 외부에서 대리인을 설정해보십시오. 아마도 당신이 원하는 것보다 늦게 일어날 것입니다. https://github.com/jerbeers/DemoLocalNotification

빌드하고 실행 버튼을 탭하면 3 초 후에 로컬 알림이 표시됩니다 :

나는 샘플 여기에 프로젝트를 추가했다. 중지하고 다시 빌드하고 실행하면 앱이 실행되는 동안에도 로컬 알림이 표시됩니다.

+0

답변 해 주셔서 감사합니다. requestAuthorization 완료 블록 전후에 위임자를 설정하려고 시도했지만 문제가 해결되지 않았습니다. 다른 솔루션을 제안 해 주시겠습니까? –

+0

걸음을 내딛게하십시오. "첫 실행"과 "다시 시작"이라고 말하면, 구체적으로 무엇을하고 있습니까? – Jerry

+0

첫 번째로 나는 의미한다. 처음으로 앱을 설치하고 '다시 시작하다'는 의미입니다. 내가 시뮬레이터에 Xcode를 설치 한 후 앱을 다시로드 할 때. –

관련 문제