//
//  NotificationService.swift
//  ServiceExtension
//
//  Created by caiyang on 2022/1/5.
//  Copyright © 2022 yin chen. All rights reserved.
//

import UserNotifications

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
//        self.contentHandler = contentHandler
        
        // 444b4a4c86a09bd2e27367fe  测试
        // c0eb5d604f19e9d58af5f8de  正式
        
        JPushNotificationExtensionService.jpushSetAppkey("444b4a4c86a09bd2e27367fe")
        JPushNotificationExtensionService.jpushReceive(request) {
            print("apns upload success")
            contentHandler(request.content)
        }
        
//        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
//
//        if let bestAttemptContent = bestAttemptContent {
//            if (bestAttemptContent.title == nil || bestAttemptContent.title == "") {
//                bestAttemptContent.title = "上药移动"
//            } else {
//                bestAttemptContent.title = "\(bestAttemptContent.title)"
//            }
//            contentHandler(bestAttemptContent)
//        }
       

        
    
    }
    
    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }

}
