Last active
January 23, 2019 06:24
-
-
Save d-date/c6bf466ced16ef6170e92e8303704e27 to your computer and use it in GitHub Desktop.
Preprocessor Token for Firebase to avoid initialize Firebase multi-times.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct FirebasePreprocessorToken { | |
public static let `default` = FirebasePreprocessorToken() | |
private init() { | |
#if RELEASE | |
let googleServiceInfoPlist = "GoogleService-Info" | |
#elseif STAGING | |
let googleServiceInfoPlist = "GoogleService-Info-Stg" | |
#else | |
let googleServiceInfoPlist = "GoogleService-Info-Dev" | |
#endif | |
guard FirebaseApp.app() == nil else { return } | |
let plist = Bundle.main.path(forResource: googleServiceInfoPlist, ofType: "plist")! | |
let firebaseOptions = FirebaseOptions(contentsOfFile: plist)! | |
FirebaseApp.configure(options: firebaseOptions) | |
} | |
} | |
// ex: For FCM | |
public class NotificationReceiver: NSObject { | |
public static let shared = NotificationReceiver() | |
public let token = PublishSubject<FCMToken>() | |
public let remoteMessage = PublishSubject<MessagingRemoteMessage>() | |
public var apnsToken: Data? { | |
return Messaging.messaging().apnsToken | |
} | |
let disposeBag = DisposeBag() | |
var subscribedTopics: Set<FCMTopicModel> | |
private init(instance: FirebasePreprocessorToken = .default) { | |
subscribedTopics = [] | |
super.init() | |
Messaging.messaging().delegate = self | |
} | |
} | |
// ex: For Google Analytics for Firebase | |
public class FirebaseAnalyticsTracker { | |
public static let shared = FirebaseAnalyticsTracker() | |
private init(token: FirebasePreprocessorToken = .default) {} | |
public func track(event: String, parameters: [String: Any] = [:]) { | |
Analytics.logEvent(event, parameters: parameters) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment