В бекграунде или в закрытом состоянии приходит 2 одинаковых PushNotification


#1

в бекграунде или в закрытом состоянии приходит 2 одинаковых PushNotification
все проверил
сервер с сервера приходит 1 пуш но на устройстве отображает 2

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
let gcmMessageIDKey = "gcm.message_id"
//    let firebaseToken = ""

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    
    UIPageControl.appearance().currentPageIndicatorTintColor = UIColor.white
    UIPageControl.appearance().pageIndicatorTintColor = UIColor.lightText
    
    FirebaseApp.configure()
    
    // [START set_messaging_delegate]
    Messaging.messaging().delegate = self
    // [END set_messaging_delegate]
    
    // Register for remote notifications. This shows a permission dialog on first run, to
    // show the dialog at a more appropriate time move this registration accordingly.
    // [START register_for_notifications]
    if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
        
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }
    
    application.registerForRemoteNotifications()
    
    // [END register_for_notifications]
    
    
    Fabric.with([Crashlytics.self])
    
    GMSServices.provideAPIKey("")
    
    return true
}

func applicationWillResignActive(_ application: UIApplication) {
    
    print("applicationWillResignActive")
    
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
    
    print("applicationDidEnterBackground")
    
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
    
    print("applicationWillEnterForeground")
    
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
    print("applicationDidBecomeActive")
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    UIApplication.shared.applicationIconBadgeNumber = 0
    
}

func applicationWillTerminate(_ application: UIApplication) {
    print("applicationWillTerminate")
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.
    
    //        CoreDataManager.instance.saveContext()
    
}



// [START receive_message]
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    
    // If you are receiving a notification message while your app is in the background,
    // this callback will not be fired till the user taps on the notification launching the application.
    // TODO: Handle data of notification
    
    // With swizzling disabled you must let Messaging know about the message, for Analytics
    // Messaging.messaging().appDidReceiveMessage(userInfo)
    
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }
    
    // Print full message.
    print(userInfo)
    print("ХЗ==================", userInfo["id"]!)
    
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    // If you are receiving a notification message while your app is in the background,
    // this callback will not be fired till the user taps on the notification launching the application.
    // TODO: Handle data of notification
    
    
    
    //         With swizzling disabled you must let Messaging know about the message, for Analytics
    //         Messaging.messaging().appDidReceiveMessage(userInfo)
    
    //
    //         1 РАЗ
    
    //         Print full message.
    print(userInfo)
    print("1 РАЗ==================", userInfo["id"]!)
    //

// let storyboard = UIStoryboard(name: “SearchSB”, bundle: nil)
// let vc = storyboard.instantiateViewController(withIdentifier: “ActiveInfoVC”) as! ActiveInfoVC
// vc.orderId = userInfo[“id”]! as! String
// window?.rootViewController = vc
//
//

    if application.applicationState == .background {
        
        completionHandler(UIBackgroundFetchResult.newData)
        
        return
    }

//
// switch application.applicationState {
//
// case .inactive:
// print(“Inactive”)
// //Show the view with the content of the push
// completionHandler(.newData)
//
// case .background:
// print(“Background”)
// //Refresh the local model
// completionHandler(.newData)
//
// case .active:
// print(“Active”)
// //Show an in-app banner
// completionHandler(.newData)
// }

}
// [END receive_message]


func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("Unable to register for remote notifications: \(error.localizedDescription)")
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    print("APNs token retrieved: \(deviceToken)")
    
    // With swizzling disabled you must set the APNs token here.
    // Messaging.messaging().apnsToken = deviceToken
}

}

// [START ios_10_message_handling]
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo
    
    // With swizzling disabled you must let Messaging know about the message, for Analytics
    // Messaging.messaging().appDidReceiveMessage(userInfo)
    
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
        
    }
    // Print full message.
    print(userInfo)
    
    
    // коли відкрито
    
    print("коли відкрито==================", userInfo["id"]!)
    
    let storyboard = UIStoryboard(name: "SearchSB", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "ActiveInfoVC") as! ActiveInfoVC
    vc.orderId = userInfo["id"]! as! String
    window?.rootViewController = vc
    
    
    
    // Change this to your preferred presentation option
    completionHandler([])
}

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }
    
    
    //по тапу з пуша
    
    // Print full message.
    print(userInfo)
    print("по тапу з пуша==================", userInfo["id"]!)
    
    let storyboard = UIStoryboard(name: "SearchSB", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "ActiveInfoVC") as! ActiveInfoVC
    vc.orderId = userInfo["id"]! as! String
    window?.rootViewController = vc
    
    completionHandler()
}

}
// [END ios_10_message_handling]

extension AppDelegate : MessagingDelegate {

// [START refresh_token]
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
    print("Firebase registration token: \(fcmToken)")
    UserDefaults.standard.setValue(fcmToken, forKey: "firebaseToken")
    //save firebaseToken
}
// [END refresh_token]

// [START ios_10_data_message]
// Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
// To enable direct data messages, you can set Messaging.messaging().shouldEstablishDirectChannel to true.
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
    print("Received data message: \(remoteMessage.appData)")
}
// [END ios_10_data_message]

}