I have it set up like this...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Added firebase for user authentication
FirebaseApp.configure()
// hijack appdelegate to redirec logged in users to explore
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = mainTabBarController()
// Request permission from user before fetching userLocation
let locationManager = userLocationListener.shared
locationManager.requestAlwaysAuthorization()
// Adds the Fabric (Crashlytics( frameworks for crash/bugs reporting
Fabric.with([Crashlytics.self])
// Register for notifications
registerForNotifications(application: application)
// Override point for customization after application launch.
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("Register for notifications: ", deviceToken)
}
// Registration token
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Registered with FCM token:", fcmToken)
}
// Listen for user notification
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.alert)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if let followerId = userInfo["followerId"] as? String {
print(followerId)
// Push new follower homeProfile
let userHomeProfile = homeController(collectionViewLayout: UICollectionViewFlowLayout())
userHomeProfile.userId = followerId
// Access mainUI from AppDelegate
if let mainTabBar = window?.rootViewController as? mainTabBarController {
// Allows access to notification window from any where in the mainTabar
mainTabBar.selectedIndex = 0
// Dismisses viewController when accessing push notification views
mainTabBar.presentedViewController?.dismiss(animated: true, completion: nil)
if let homeProfile = mainTabBar.viewControllers?.first as? UINavigationController {
homeProfile.pushViewController(userHomeProfile, animated: true)
}
}
}
}
// APNS setup
private func registerForNotifications(application: UIApplication) {
print("Attempting to register APNS...")
// Notifies self when register is successful
Messaging.messaging().delegate = self
// Set delegate
UNUserNotificationCenter.current().delegate = self
// User notifications auth
// target IOs 10+
let options: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: options) { (granted, error) in
// Returns error on request
if let error = error {
print("Failed to request auth:", error)
return
}
// Auth request is granted
if granted {
print("Auth granted")
} else {
// Auth is denied
print("Auth denied")
}
}
application.registerForRemoteNotifications()
}