Skip to content

Instantly share code, notes, and snippets.

@seanli1 seanli1/AppDelegate.swift
Last active May 23, 2019

Embed
What would you like to do?
AppDelegate.swift starter code with Firebase initialization that minimizes print statements and sets up remote notifications with perks: notify when app is in foreground, and clear badge on open.
//
// AppDelegate.swift
//
//
// Created by Sean Li on 5/22/19.
// Copyright © 2019 Sean Li. All rights reserved.
//
import UIKit
import Firebase
let firebaseApp = FirebaseApp.app()
let firestore = Firestore.firestore(app: firebaseApp!)
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
initializeFirebase(application: application)
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = ViewController()
return true
}
func initializeFirebase(application: UIApplication) {
// Configure Firebase
FirebaseConfiguration.shared.setLoggerLevel(.warning)
FirebaseApp.configure()
// Configure Messaging for remote notifications
Messaging.messaging().delegate = self
if #available(iOS 10.0, *) {
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()
}
func applicationWillResignActive(_ application: UIApplication) {
// 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) {
// 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) {
// 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) {
// 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
// Put this here, and not in applicationWillEnterForeground. That's because this runs every time the app will become active (including on boot), whereas applicationWillEnterForeground only runs when the app goes from background to foreground.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
// Use this delegate method if needed to get Firebase stuff.
}
// Show notification even if app is in foreground. Without this, it would only show if in the background.
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.