diff --git a/GoMoney/AppDelegate/AppDelegate.swift b/GoMoney/AppDelegate/AppDelegate.swift index 0b30cec..691ecaf 100644 --- a/GoMoney/AppDelegate/AppDelegate.swift +++ b/GoMoney/AppDelegate/AppDelegate.swift @@ -1,18 +1,41 @@ import FirebaseCore +import FirebaseMessaging import GoogleSignIn import UIKit +import UserNotifications @main class AppDelegate: UIResponder, UIApplicationDelegate { + let gcmMessageIDKey = "gcm.message_id" + func application(_: UIApplication, open url: URL, options _: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool { return GIDSignIn.sharedInstance.handle(url) } - func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. FirebaseApp.configure() + Messaging.messaging().delegate = self + + if #available(iOS 10.0, *) { + // For iOS 10 display notification (sent via APNS) + UNUserNotificationCenter.current().delegate = self + + let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] + UNUserNotificationCenter.current().requestAuthorization( + options: authOptions, + completionHandler: { _, _ in } + ) + } else { + let settings: UIUserNotificationSettings = + .init(types: [.alert, .badge, .sound], categories: nil) + application.registerUserNotificationSettings(settings) + } + + application.registerForRemoteNotifications() + return true } @@ -29,4 +52,78 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. // Use this method to release any resources that were specific to the discarded scenes, as they will not return. } + + func application(_: UIApplication, + didReceiveRemoteNotification userInfo: [AnyHashable: Any]) + { + if let messageID = userInfo[gcmMessageIDKey] { + print("Message ID: \(messageID)") + } + print(userInfo) + } + + func application(_: UIApplication, + didReceiveRemoteNotification userInfo: [AnyHashable: Any]) async + -> UIBackgroundFetchResult + { + if let messageID = userInfo[gcmMessageIDKey] { + print("Message ID: \(messageID)") + } + print(userInfo) + + return UIBackgroundFetchResult.newData + } + + func application(_: UIApplication, + didFailToRegisterForRemoteNotificationsWithError error: Error) + { + print("Unable to register for remote notifications: \(error.localizedDescription)") + } + + func application(_: UIApplication, + didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) + { + print("APNs token retrieved: \(deviceToken)") + } +} + +extension AppDelegate: UNUserNotificationCenterDelegate { + func userNotificationCenter(_: UNUserNotificationCenter, + willPresent notification: UNNotification) async + -> UNNotificationPresentationOptions + { + let userInfo = notification.request.content.userInfo + + if let messageID = userInfo[gcmMessageIDKey] { + print("Message ID: \(messageID)") + } + + print(userInfo) + return [[.alert, .sound]] + } + + func userNotificationCenter(_: UNUserNotificationCenter, + didReceive response: UNNotificationResponse) async + { + let userInfo = response.notification.request.content.userInfo + + if let messageID = userInfo[gcmMessageIDKey] { + print("Message ID: \(messageID)") + } + print(userInfo) + } +} + +extension AppDelegate: MessagingDelegate { + func messaging(_: Messaging, didReceiveRegistrationToken fcmToken: String?) { + print("Firebase registration token: \(String(describing: fcmToken))") + + let dataDict: [String: String] = ["token": fcmToken ?? ""] + NotificationCenter.default.post( + name: Notification.Name("FCMToken"), + object: nil, + userInfo: dataDict + ) + // TODO: If necessary send token to application server. + } } diff --git a/GoMoney/GoMoney.entitlements b/GoMoney/GoMoney.entitlements index 8fd3a3d..a7de1d4 100644 --- a/GoMoney/GoMoney.entitlements +++ b/GoMoney/GoMoney.entitlements @@ -2,6 +2,8 @@ + aps-environment + development com.apple.security.application-groups group.kappa.expense diff --git a/GoMoney/Info.plist b/GoMoney/Info.plist index 35ca653..486d824 100644 --- a/GoMoney/Info.plist +++ b/GoMoney/Info.plist @@ -35,6 +35,11 @@ + UIBackgroundModes + + fetch + remote-notification + UIFileSharingEnabled diff --git a/Podfile b/Podfile index e5f2e44..5f04929 100644 --- a/Podfile +++ b/Podfile @@ -8,6 +8,7 @@ pod 'FirebaseCore' pod 'FirebaseAuth' pod 'FirebaseFirestore' pod 'FirebaseFirestoreSwift' +pod 'FirebaseMessaging' pod 'Charts', :git => 'https://github.com/danielgindi/Charts.git', :branch => 'master' pod 'RealmSwift', '~> 10.32' diff --git a/Podfile.lock b/Podfile.lock index 41b3ffd..8797a9a 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -645,8 +645,26 @@ PODS: - FirebaseCoreExtension (~> 10.0) - FirebaseFirestore (~> 10.0) - FirebaseSharedSwift (~> 10.0) + - FirebaseInstallations (10.0.0): + - FirebaseCore (~> 10.0) + - GoogleUtilities/Environment (~> 7.8) + - GoogleUtilities/UserDefaults (~> 7.8) + - PromisesObjC (~> 2.1) + - FirebaseMessaging (10.2.0): + - FirebaseCore (~> 10.0) + - FirebaseInstallations (~> 10.0) + - GoogleDataTransport (~> 9.2) + - GoogleUtilities/AppDelegateSwizzler (~> 7.8) + - GoogleUtilities/Environment (~> 7.8) + - GoogleUtilities/Reachability (~> 7.8) + - GoogleUtilities/UserDefaults (~> 7.8) + - nanopb (< 2.30910.0, >= 2.30908.0) - FirebaseSharedSwift (10.1.0) - Floaty (4.2.0) + - GoogleDataTransport (9.2.0): + - GoogleUtilities/Environment (~> 7.7) + - nanopb (< 2.30910.0, >= 2.30908.0) + - PromisesObjC (< 3.0, >= 1.2) - GoogleSignIn (6.2.4): - AppAuth (~> 1.5) - GTMAppAuth (~> 1.3) @@ -666,6 +684,8 @@ PODS: - "GoogleUtilities/NSData+zlib (7.8.0)" - GoogleUtilities/Reachability (7.8.0): - GoogleUtilities/Logger + - GoogleUtilities/UserDefaults (7.8.0): + - GoogleUtilities/Logger - GradientLoadingBar (3.0.0) - "gRPC-C++ (1.44.0)": - "gRPC-C++/Implementation (= 1.44.0)" @@ -757,6 +777,7 @@ DEPENDENCIES: - FirebaseCore - FirebaseFirestore - FirebaseFirestoreSwift + - FirebaseMessaging - Floaty - GoogleSignIn - GradientLoadingBar (~> 3.0) @@ -780,8 +801,11 @@ SPEC REPOS: - FirebaseCoreInternal - FirebaseFirestore - FirebaseFirestoreSwift + - FirebaseInstallations + - FirebaseMessaging - FirebaseSharedSwift - Floaty + - GoogleDataTransport - GoogleSignIn - GoogleUtilities - GradientLoadingBar @@ -826,8 +850,11 @@ SPEC CHECKSUMS: FirebaseCoreInternal: 5eb3960335da5ea30115d57d39db6988c4ad06f3 FirebaseFirestore: 5007583f3db2129de8e87f18ee63f4c86f07e7a3 FirebaseFirestoreSwift: ba0e8ae1dca6cd395c646671a7fe2dcc077db225 + FirebaseInstallations: 7f1c9ae6bd9df6abe9c74124b38fa8740aba5df4 + FirebaseMessaging: cc9f40f5b7494680f3844d08e517e92aa4e8d9f7 FirebaseSharedSwift: 6966c4de41fba13a4270de1e421e6eee2cf90113 Floaty: e2bd5a0f6f7f70899e26ff2da31081c8bee8d1b0 + GoogleDataTransport: 1c8145da7117bd68bbbed00cf304edb6a24de00f GoogleSignIn: 5651ce3a61e56ca864160e79b484cd9ed3f49b7a GoogleUtilities: 1d20a6ad97ef46f67bbdec158ce00563a671ebb7 GradientLoadingBar: 3c4246535efdedaf9b471c05caabfdb4b87b40a2 @@ -849,6 +876,6 @@ SPEC CHECKSUMS: SwiftAlgorithms: 38dda4731d19027fdeee1125f973111bf3386b53 TTGSnackbar: fea91c62637b5662f808a4c3b55f7631cb7e1ef7 -PODFILE CHECKSUM: 71ebaf0f62dbee208320c056eb7a0a0f27da3d2e +PODFILE CHECKSUM: 22ee40650525751f14ffe79cbbdb8ee424a7ed85 COCOAPODS: 1.11.3