Skip to content

Commit

Permalink
Added firebase messaging to iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
cindy-x-liang committed Oct 24, 2024
1 parent 398c5cb commit caf8aaf
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 5 deletions.
1 change: 1 addition & 0 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ target 'TCAT' do
# Analytics
pod 'Firebase'
pod 'FirebaseCrashlytics'
pod 'Firebase/Messaging'

# File Management
pod 'Zip', '~> 1.1'
Expand Down
17 changes: 16 additions & 1 deletion Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ PODS:
- FirebaseAnalytics (~> 10.24.0)
- Firebase/CoreOnly (10.24.0):
- FirebaseCore (= 10.24.0)
- Firebase/Messaging (10.24.0):
- Firebase/CoreOnly
- FirebaseMessaging (~> 10.24.0)
- FirebaseAnalytics (10.24.0):
- FirebaseAnalytics/AdIdSupport (= 10.24.0)
- FirebaseCore (~> 10.0)
Expand Down Expand Up @@ -50,6 +53,15 @@ PODS:
- GoogleUtilities/Environment (~> 7.8)
- GoogleUtilities/UserDefaults (~> 7.8)
- PromisesObjC (~> 2.1)
- FirebaseMessaging (10.24.0):
- FirebaseCore (~> 10.0)
- FirebaseInstallations (~> 10.0)
- GoogleDataTransport (~> 9.3)
- GoogleUtilities/AppDelegateSwizzler (~> 7.8)
- GoogleUtilities/Environment (~> 7.8)
- GoogleUtilities/Reachability (~> 7.8)
- GoogleUtilities/UserDefaults (~> 7.8)
- nanopb (< 2.30911.0, >= 2.30908.0)
- FirebaseRemoteConfigInterop (10.24.0)
- FirebaseSessions (10.24.0):
- FirebaseCore (~> 10.5)
Expand Down Expand Up @@ -142,6 +154,7 @@ DEPENDENCIES:
- Apollo (~> 1.9.3)
- DZNEmptyDataSet (from `https://github.com/cuappdev/DZNEmptyDataSet.git`)
- Firebase
- Firebase/Messaging
- FirebaseCrashlytics
- FutureNova (from `https://github.com/cuappdev/ios-networking.git`)
- GoogleMaps
Expand All @@ -165,6 +178,7 @@ SPEC REPOS:
- FirebaseCoreInternal
- FirebaseCrashlytics
- FirebaseInstallations
- FirebaseMessaging
- FirebaseRemoteConfigInterop
- FirebaseSessions
- GoogleAppMeasurement
Expand Down Expand Up @@ -213,6 +227,7 @@ SPEC CHECKSUMS:
FirebaseCoreInternal: bcb5acffd4ea05e12a783ecf835f2210ce3dc6af
FirebaseCrashlytics: af38ea4adfa606f6e63fcc22091b61e7938fcf66
FirebaseInstallations: 8f581fca6478a50705d2bd2abd66d306e0f5736e
FirebaseMessaging: 4d52717dd820707cc4eadec5eb981b4832ec8d5d
FirebaseRemoteConfigInterop: 6c349a466490aeace3ce9c091c86be1730711634
FirebaseSessions: 2651b464e241c93fd44112f995d5ab663c970487
FutureNova: 95f9aa352b2c250253b96fdf380754afcc87c7f3
Expand All @@ -234,6 +249,6 @@ SPEC CHECKSUMS:
Wormholy: ab1c8c2f02f58587a0941deb0088555ffbf039a1
Zip: 8877eede3dda76bcac281225c20e71c25270774c

PODFILE CHECKSUM: a3b80dd04ea30998a17c032f2730e21ee8517238
PODFILE CHECKSUM: 03571a87e3df2cb79c3c62b5bd19cd6713131c52

COCOAPODS: 1.15.2
12 changes: 10 additions & 2 deletions TCAT.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
BF74AC1E1F945D8E00AFD4E4 /* GoogleMaps.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BF74AC1C1F945D8E00AFD4E4 /* GoogleMaps.framework */; };
D4756EA223986CB500FE7F0D /* ReachabilityManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4756EA123986CB500FE7F0D /* ReachabilityManager.swift */; };
DD3D9C211F94297100B164D4 /* Reachability.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD3D9C201F94297100B164D4 /* Reachability.swift */; };
EEB26AE22C9F9B9A002E863F /* UserNotifications.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EEB26AE12C9F9B9A002E863F /* UserNotifications.framework */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -271,6 +272,9 @@
BF74AC1C1F945D8E00AFD4E4 /* GoogleMaps.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GoogleMaps.framework; path = Pods/GoogleMaps/Maps/Frameworks/GoogleMaps.framework; sourceTree = "<group>"; };
D4756EA123986CB500FE7F0D /* ReachabilityManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReachabilityManager.swift; sourceTree = "<group>"; };
DD3D9C201F94297100B164D4 /* Reachability.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Reachability.swift; sourceTree = "<group>"; };
EEB26AE02C9F998C002E863F /* TCATLocal.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = TCATLocal.entitlements; sourceTree = "<group>"; };
EEB26AE12C9F9B9A002E863F /* UserNotifications.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UserNotifications.framework; path = System/Library/Frameworks/UserNotifications.framework; sourceTree = SDKROOT; };
EEB26AE32C9FA60E002E863F /* TCATDebug.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = TCATDebug.entitlements; sourceTree = "<group>"; };
FD69AF2A2B89212F00970C7E /* ci_post_clone.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = ci_post_clone.sh; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand All @@ -279,6 +283,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
EEB26AE22C9F9B9A002E863F /* UserNotifications.framework in Frameworks */,
BF74AC1D1F945D8E00AFD4E4 /* GoogleMapsCore.framework in Frameworks */,
BF74AC1E1F945D8E00AFD4E4 /* GoogleMaps.framework in Frameworks */,
BF74AC1A1F945D7D00AFD4E4 /* GoogleMapsBase.framework in Frameworks */,
Expand All @@ -292,6 +297,7 @@
12F774CEB5023E6938BDCF3A /* Frameworks */ = {
isa = PBXGroup;
children = (
EEB26AE12C9F9B9A002E863F /* UserNotifications.framework */,
BF74AC1C1F945D8E00AFD4E4 /* GoogleMaps.framework */,
BF74AC1B1F945D8E00AFD4E4 /* GoogleMapsCore.framework */,
BF74AC191F945D7D00AFD4E4 /* GoogleMapsBase.framework */,
Expand Down Expand Up @@ -575,6 +581,8 @@
449A7C771D80D0E80019300C /* TCAT */ = {
isa = PBXGroup;
children = (
EEB26AE32C9FA60E002E863F /* TCATDebug.entitlements */,
EEB26AE02C9F998C002E863F /* TCATLocal.entitlements */,
449A7C7F1D80D0E80019300C /* Assets.xcassets */,
2E70434D2BB75E10003AC1D6 /* PrivacyInfo.xcprivacy */,
2E9416662BC615B0003DEB44 /* Base */,
Expand Down Expand Up @@ -1077,7 +1085,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
BUNDLE_APP_DISPLAY_NAME = "Ithaca Transit Beta";
CODE_SIGN_ENTITLEMENTS = TCAT/Supporting/TCAT.entitlements;
CODE_SIGN_ENTITLEMENTS = TCAT/TCATDebug.entitlements;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
Expand Down Expand Up @@ -1176,7 +1184,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
BUNDLE_APP_DISPLAY_NAME = "Ithaca Transit Local";
CODE_SIGN_ENTITLEMENTS = TCAT/Supporting/TCAT.entitlements;
CODE_SIGN_ENTITLEMENTS = TCAT/TCATLocal.entitlements;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
Expand Down
69 changes: 67 additions & 2 deletions TCAT/Base/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import Intents
import SafariServices
import SwiftyJSON
import UIKit
import FirebaseMessaging

/// This is used for app-specific preferences
let userDefaults = UserDefaults.standard

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {

var window: UIWindow?
private let encoder = JSONEncoder()
Expand All @@ -36,7 +37,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

// Set Up Google Services
FirebaseApp.configure()

GMSServices.provideAPIKey(TransitEnvironment.googleMaps)

// Update shortcut items
Expand Down Expand Up @@ -90,13 +91,49 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()

//Set up notifications
UNUserNotificationCenter.current().delegate = self

let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: { _, _ in }
)

application.registerForRemoteNotifications()


Messaging.messaging().delegate = self







return true
}

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
handleShortcut(item: shortcutItem)
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
Messaging.messaging().token { token, error in
if let error = error {
print("Error fetching FCM registration token: \(error)")
} else if let token = token {
print("FCM registration token: \(token)")

}
}

}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("application didFailToRegisterForRemoteNotificationsWithError")
}

// MARK: - Helper Functions

Expand Down Expand Up @@ -206,3 +243,31 @@ extension UIWindow {
}

}

extension AppDelegate {

//MessagingDelegate
// func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
// self.firebaseToken = fcmToken!
// print("Firebase token: \(fcmToken)")
// }

func messaging(_ 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.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}

//UNUserNotificationCenterDelegate
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("APNs received with: \(userInfo)")
}

}
2 changes: 2 additions & 0 deletions TCAT/Supporting/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@
</array>
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>location</string>
<string>remote-notification</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
Expand Down
2 changes: 2 additions & 0 deletions TCAT/Supporting/TCAT.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>aps-environment</key>
<string>development</string>
<key>com.apple.security.application-groups</key>
<array>
<string>group.tcat</string>
Expand Down
12 changes: 12 additions & 0 deletions TCAT/TCATDebug.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>aps-environment</key>
<string>development</string>
<key>com.apple.security.application-groups</key>
<array>
<string>group.tcat</string>
</array>
</dict>
</plist>
12 changes: 12 additions & 0 deletions TCAT/TCATLocal.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>aps-environment</key>
<string>development</string>
<key>com.apple.security.application-groups</key>
<array>
<string>group.tcat</string>
</array>
</dict>
</plist>

0 comments on commit caf8aaf

Please sign in to comment.