Я получаю уведомления без проблем во всех версиях IOS до IOS 12. В IOS 12 я не получаю уведомлений.
Я знаю, что где-то читал, что в Firebase была ошибка, но я больше не могу найти этот пост. Приложение запущено, и пользователи начали сообщать, что не получают уведомлений.
Using Firebase (5.11.0)
Using FirebaseAnalytics (5.3.0)
Using FirebaseAnalyticsInterop (1.1.0)
Using FirebaseAuth (5.0.4)
Using FirebaseAuthInterop (1.0.0)
Using FirebaseCore (5.1.6)
Using FirebaseDatabase (5.0.3)
Using FirebaseDynamicLinks (3.1.1)
Using FirebaseInstanceID (3.3.0)
Using FirebaseMessaging (3.2.1)
Using FirebaseStorage (3.0.2)
Using FirebaseUI (5.2.0)
import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
import FirebaseCore
import FirebaseMessaging
import FirebaseInstanceID
import UserNotifications
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
let badgeCountKey = "badgeCountKey" //the app badge count for notification
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
// iOS 10 support
if #available(iOS 10, *) {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in
}
application.registerForRemoteNotifications()
}
// iOS 9 support
else if #available(iOS 9, *) {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
// iOS 8 support
else if #available(iOS 8, *) {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
// iOS 7 support
else {
application.registerForRemoteNotifications(matching: [.badge, .sound, .alert])
}
NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification(notification:)), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)
// - check if app is launched from notification
// - If app wasn’t running and user launches it by tapping the push notification
if let notification = launchOptions?[.remoteNotification] as? [String: AnyObject] {
prepareToParse(notification: notification)
}
return true
} //end of didFinishLaunchingWithOptions
func tokenRefreshNotification(notification: NSNotification) {
connectToFcm()
let refereshToken = InstanceID.instanceID().token()
guard let token = refereshToken else {return} } //end tokenRefreshNotification
func connectToFcm() {
Messaging.messaging().connect { (error) in
if error != nil {
print("unable to connect to FCM")
}else {
print("connected to FCM")
}
}
}
func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register: \(error)")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
guard let aps = userInfo["aps"] as? [String:Any] else {
printsNow(message: "notification not received")
return
}
guard let alert = aps["alert"] as? [String : Any] else {
printsNow(message: "alert does not contain any info ")
return
}
_ = NewsItem.makeNewsItem(alert)
let previousCount = UserDefaults.standard.value(forKey: badgeCountKey) as! Int
let currentCount = previousCount + 1
UserDefaults.standard.setValue(currentCount, forKey: badgeCountKey)
UIApplication.shared.applicationIconBadgeNumber = currentCount
(window?.rootViewController as? UITabBarController)?.selectedIndex = 1
}
//display notifications when app is in foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
guard let aps = userInfo["aps"] as? [String:Any] else {
printsNow(message: "notification not received")
return
}
guard let alert = aps["alert"] as? [String : Any] else {
printsNow(message: "alert does not contain any info ")
return
}
let _ = NewsItem.makeNewsItem(alert)
let previousCount = UserDefaults.standard.value(forKey: badgeCountKey) as! Int
let currentCount = previousCount + 1
UserDefaults.standard.setValue(currentCount, forKey: badgeCountKey)
UIApplication.shared.applicationIconBadgeNumber = currentCount
if let tabBarController = window?.rootViewController as? UITabBarController {
if let selectedVC = tabBarController.selectedViewController as? UINavigationController {
if let lastVC = selectedVC.viewControllers.last as? ChatViewController {
for (key, val) in userInfo {
if key as? String == "gcm.notification.chatUID" {
guard let chatID = val as? String else {return}
if lastVC.chatSelected.chatUID == chatID {
print("chatUID \(chatID) is true")
completionHandler([]) //don't show notification because the message is for the current presented viewController
return
}
}
}
}
}
}
completionHandler([.alert, .badge, .sound])
}
//when app is in foreground and you click on notification
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
(window?.rootViewController as? UITabBarController)?.selectedIndex = 1
}
} //end appDelegate