UNResponseManager.swift 1.7キロバイト
//
//  UserNofiticationManager.swift
//  ko-seigen
//
//  Created by USER on 2017/10/04.
//  Copyright © 2017年 ドラッサル 亜嵐. All rights reserved.
//

import Foundation
//
//  UNNofiticationResponse.swift
//  ko-seigen
//
//  Created by USER on 2017/10/04.
//  Copyright © 2017年 ドラッサル 亜嵐. All rights reserved.
//

import Foundation
import UserNotifications

class UNResponseManager:NSObject, UNUserNotificationCenterDelegate{
    
    static let sharedInstance =  UNResponseManager();
    private var Response:[String:((_ actionIdentifier:String) -> Void )] = [:]
    
    public func setResponse( identifier:String,response:@escaping ((_ actionIdentifier:String) -> Void ))
        ->UNResponseManager{
            if(Response[identifier] == nil){
                Response[identifier] = response
            }
            return self
    }
    
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert,.sound])
        NSLog("called")
    }
    
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        
        let id = response.notification.request.content.categoryIdentifier
        if(Response[id] != nil){
            Response[id]!(response.actionIdentifier)
        }
        completionHandler()
    }
    
}