2016-10-05 1 views
1

를 사용하는 방법, 내가 이것을 사용하는 방법에 대한 샘플을 찾을 수 없습니다아이폰 OS 10에서 UNNotificationPresentationOptions

하지만 응용 프로그램은 UNNotificationPresentationOptions를 사용하여 포 그라운드에있을 때 알림을 표시하기위한 옵션이,이, 제안하십시오 이 기능을 구현하는 방법에 대한 아이디어

답변

3

UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound]) {(accepted, error) in 

      if !accepted { 
       print("Notification access denied") 
      }    
     } 
+0

'userNotificationCenter (_ : didReceive : withCompletionHandler :)'의 끝에서'completionHandler()'를 호출하는 것을 잊지 마십시오. 전화하지 않으면 원치 않는 행동을하게됩니다. 자세한 내용은 [the docs] (https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate/1649501-usernotificationcenter)를 참조하십시오. –

0

새로운 iOS 10 UNUserNotificationCenterDelegate에는 이제 원격 및 로컬 알림을 처리하는 단일 세트의 메소드가 있습니다.

UNUserNotificationCenterDelegate 프로토콜 : 앱이 주어진 통지를 사용자가 선택 된 작업을 알려 호출

userNotificationCenter(_:didReceive:withCompletionHandler:)

.

userNotificationCenter(_:willPresent:withCompletionHandler:)

포 그라운드에서 실행되는 응용 프로그램에 알림을 제공합니다.

두 가지 방법. 더 나은 점은 이제 자신의 프로토콜 인 iOS 10

UNUserNotificationCenterDelegate으로 옮겨 졌으므로 기존의 알림 처리 코드를 모두 반짝이는 새 기능으로 리팩터링하여 기존 UIApplicationDelegat을 정리하는 데 도움이됩니다. 그것의 응집력있는 프로토콜은 자신의 것입니다. 여기

은 예입니다 : 내가 didFinishLaunchingWithOptions 내 AppDelegate에 내의 ViewController

extension UIViewController: UNUserNotificationCenterDelegate { 

    public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) { 
     completionHandler([.alert, .badge, .sound]) 
    } 

    public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Swift.Void) { 
     print("Do what ever you want") 

    } 

} 

에 아래 코드를 추가

에 의해 전경 통지를 구현 한

extension NotificationManager: UNUserNotificationCenterDelegate { 

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler:() -> Void) { 

    switch response.actionIdentifier { 

    // NotificationActions is a custom String enum I've defined 
    case NotificationActions.HighFive.rawValue: 
     print("High Five Delivered!") 
    default: break 
    } 
} 

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { 

    // Delivers a notification to an app running in the foreground. 
} 
}