Сделайте что-то подобное в AppDelegate, вне его тела.как его расширение.
extension AppDelegate : MessagingDelegate {
func application(received remoteMessage: MessagingRemoteMessage) {
print("remoteMessage: MessagingRemoteMessage")
}
}
extension AppDelegate : UNUserNotificationCenterDelegate {
// MARK: - UNUserNotificationCenterDelegate
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler:
@escaping (UNNotificationPresentationOptions) -> Void) {
// Always show the incoming notification, even if the app is in foreground
print("Received notification in foreground:")
//let jsonString = notification.request.content.userInfo.jsonString ?? "{}"
//print("\(jsonString)")
completionHandler([.alert, .badge, .sound])
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Received notification response")
//let jsonString = response.notification.request.content.userInfo.jsonString ?? "{}"
//print("\(jsonString)")
userInfo = response.notification.request.content.userInfo
NotificationsService.shared.didReceiveNotificationFromBackground(userinfo: userInfo)
completionHandler()
}
}
Внутри AppDelegate
// MARK: - Push Notification Code
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)
print(userInfo)
if application.applicationState == UIApplicationState.inactive || application.applicationState == UIApplicationState.background || application.applicationState == UIApplicationState.active {
if application.applicationState == UIApplicationState.background {
NotificationsService.shared.didReceiveNotificationFromBackground(userinfo: userInfo)
}
else if application.applicationState == UIApplicationState.inactive {
NotificationsService.shared.didReceiveNotificationFromBackground(userinfo: userInfo)
}
else if application.applicationState == UIApplicationState.active {
NotificationsService.shared.didReceiveNotificationFromBackground(userinfo: userInfo)
}
}
completionHandler(UIBackgroundFetchResult.newData)
}
// MARK: - Delegates Method For Push Notifications
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
if application.applicationState == UIApplicationState.inactive || application.applicationState == UIApplicationState.background || application.applicationState == UIApplicationState.active {
//opened from a push notification when the app was on background
if application.applicationState == UIApplicationState.background {
NotificationsService.shared.didReceiveNotificationFromBackground(userinfo: userInfo)
}
else if application.applicationState == UIApplicationState.inactive {
NotificationsService.shared.didReceiveNotificationFromBackground(userinfo: userInfo)
}else if application.applicationState == UIApplicationState.active {
if self.isAlreadyInConsult == false {
NotificationsService.shared.didReceiveNotificationFromBackground(userinfo: userInfo)
}
}
}
}
Также добавьте в свой проект plist .
FirebaseAppDelegateProxyEnabled = NO