Skip to content

Commit 6f6a88a

Browse files
feat: add push notification (#66)
1 parent 52e978a commit 6f6a88a

File tree

5 files changed

+134
-2
lines changed

5 files changed

+134
-2
lines changed

GoMoney/AppDelegate/AppDelegate.swift

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
11
import FirebaseCore
2+
import FirebaseMessaging
23
import GoogleSignIn
34
import UIKit
5+
import UserNotifications
46

57
@main
68
class AppDelegate: UIResponder, UIApplicationDelegate {
9+
let gcmMessageIDKey = "gcm.message_id"
10+
711
func application(_: UIApplication, open url: URL, options _: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
812
return GIDSignIn.sharedInstance.handle(url)
913
}
1014

11-
func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
15+
func application(_ application: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
1216
// Override point for customization after application launch.
1317

1418
FirebaseApp.configure()
1519

20+
Messaging.messaging().delegate = self
21+
22+
if #available(iOS 10.0, *) {
23+
// For iOS 10 display notification (sent via APNS)
24+
UNUserNotificationCenter.current().delegate = self
25+
26+
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
27+
UNUserNotificationCenter.current().requestAuthorization(
28+
options: authOptions,
29+
completionHandler: { _, _ in }
30+
)
31+
} else {
32+
let settings: UIUserNotificationSettings =
33+
.init(types: [.alert, .badge, .sound], categories: nil)
34+
application.registerUserNotificationSettings(settings)
35+
}
36+
37+
application.registerForRemoteNotifications()
38+
1639
return true
1740
}
1841

@@ -29,4 +52,78 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2952
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
3053
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
3154
}
55+
56+
func application(_: UIApplication,
57+
didReceiveRemoteNotification userInfo: [AnyHashable: Any])
58+
{
59+
if let messageID = userInfo[gcmMessageIDKey] {
60+
print("Message ID: \(messageID)")
61+
}
62+
print(userInfo)
63+
}
64+
65+
func application(_: UIApplication,
66+
didReceiveRemoteNotification userInfo: [AnyHashable: Any]) async
67+
-> UIBackgroundFetchResult
68+
{
69+
if let messageID = userInfo[gcmMessageIDKey] {
70+
print("Message ID: \(messageID)")
71+
}
72+
print(userInfo)
73+
74+
return UIBackgroundFetchResult.newData
75+
}
76+
77+
func application(_: UIApplication,
78+
didFailToRegisterForRemoteNotificationsWithError error: Error)
79+
{
80+
print("Unable to register for remote notifications: \(error.localizedDescription)")
81+
}
82+
83+
func application(_: UIApplication,
84+
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
85+
{
86+
print("APNs token retrieved: \(deviceToken)")
87+
}
88+
}
89+
90+
extension AppDelegate: UNUserNotificationCenterDelegate {
91+
func userNotificationCenter(_: UNUserNotificationCenter,
92+
willPresent notification: UNNotification) async
93+
-> UNNotificationPresentationOptions
94+
{
95+
let userInfo = notification.request.content.userInfo
96+
97+
if let messageID = userInfo[gcmMessageIDKey] {
98+
print("Message ID: \(messageID)")
99+
}
100+
101+
print(userInfo)
102+
return [[.alert, .sound]]
103+
}
104+
105+
func userNotificationCenter(_: UNUserNotificationCenter,
106+
didReceive response: UNNotificationResponse) async
107+
{
108+
let userInfo = response.notification.request.content.userInfo
109+
110+
if let messageID = userInfo[gcmMessageIDKey] {
111+
print("Message ID: \(messageID)")
112+
}
113+
print(userInfo)
114+
}
115+
}
116+
117+
extension AppDelegate: MessagingDelegate {
118+
func messaging(_: Messaging, didReceiveRegistrationToken fcmToken: String?) {
119+
print("Firebase registration token: \(String(describing: fcmToken))")
120+
121+
let dataDict: [String: String] = ["token": fcmToken ?? ""]
122+
NotificationCenter.default.post(
123+
name: Notification.Name("FCMToken"),
124+
object: nil,
125+
userInfo: dataDict
126+
)
127+
// TODO: If necessary send token to application server.
128+
}
32129
}

GoMoney/GoMoney.entitlements

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5+
<key>aps-environment</key>
6+
<string>development</string>
57
<key>com.apple.security.application-groups</key>
68
<array>
79
<string>group.kappa.expense</string>

GoMoney/Info.plist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
</array>
3636
</dict>
3737
</dict>
38+
<key>UIBackgroundModes</key>
39+
<array>
40+
<string>fetch</string>
41+
<string>remote-notification</string>
42+
</array>
3843
<key>UIFileSharingEnabled</key>
3944
<true/>
4045
</dict>

Podfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pod 'FirebaseCore'
88
pod 'FirebaseAuth'
99
pod 'FirebaseFirestore'
1010
pod 'FirebaseFirestoreSwift'
11+
pod 'FirebaseMessaging'
1112

1213
pod 'Charts', :git => 'https://github.com/danielgindi/Charts.git', :branch => 'master'
1314
pod 'RealmSwift', '~> 10.32'

Podfile.lock

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,26 @@ PODS:
645645
- FirebaseCoreExtension (~> 10.0)
646646
- FirebaseFirestore (~> 10.0)
647647
- FirebaseSharedSwift (~> 10.0)
648+
- FirebaseInstallations (10.0.0):
649+
- FirebaseCore (~> 10.0)
650+
- GoogleUtilities/Environment (~> 7.8)
651+
- GoogleUtilities/UserDefaults (~> 7.8)
652+
- PromisesObjC (~> 2.1)
653+
- FirebaseMessaging (10.2.0):
654+
- FirebaseCore (~> 10.0)
655+
- FirebaseInstallations (~> 10.0)
656+
- GoogleDataTransport (~> 9.2)
657+
- GoogleUtilities/AppDelegateSwizzler (~> 7.8)
658+
- GoogleUtilities/Environment (~> 7.8)
659+
- GoogleUtilities/Reachability (~> 7.8)
660+
- GoogleUtilities/UserDefaults (~> 7.8)
661+
- nanopb (< 2.30910.0, >= 2.30908.0)
648662
- FirebaseSharedSwift (10.1.0)
649663
- Floaty (4.2.0)
664+
- GoogleDataTransport (9.2.0):
665+
- GoogleUtilities/Environment (~> 7.7)
666+
- nanopb (< 2.30910.0, >= 2.30908.0)
667+
- PromisesObjC (< 3.0, >= 1.2)
650668
- GoogleSignIn (6.2.4):
651669
- AppAuth (~> 1.5)
652670
- GTMAppAuth (~> 1.3)
@@ -666,6 +684,8 @@ PODS:
666684
- "GoogleUtilities/NSData+zlib (7.8.0)"
667685
- GoogleUtilities/Reachability (7.8.0):
668686
- GoogleUtilities/Logger
687+
- GoogleUtilities/UserDefaults (7.8.0):
688+
- GoogleUtilities/Logger
669689
- GradientLoadingBar (3.0.0)
670690
- "gRPC-C++ (1.44.0)":
671691
- "gRPC-C++/Implementation (= 1.44.0)"
@@ -757,6 +777,7 @@ DEPENDENCIES:
757777
- FirebaseCore
758778
- FirebaseFirestore
759779
- FirebaseFirestoreSwift
780+
- FirebaseMessaging
760781
- Floaty
761782
- GoogleSignIn
762783
- GradientLoadingBar (~> 3.0)
@@ -780,8 +801,11 @@ SPEC REPOS:
780801
- FirebaseCoreInternal
781802
- FirebaseFirestore
782803
- FirebaseFirestoreSwift
804+
- FirebaseInstallations
805+
- FirebaseMessaging
783806
- FirebaseSharedSwift
784807
- Floaty
808+
- GoogleDataTransport
785809
- GoogleSignIn
786810
- GoogleUtilities
787811
- GradientLoadingBar
@@ -826,8 +850,11 @@ SPEC CHECKSUMS:
826850
FirebaseCoreInternal: 5eb3960335da5ea30115d57d39db6988c4ad06f3
827851
FirebaseFirestore: 5007583f3db2129de8e87f18ee63f4c86f07e7a3
828852
FirebaseFirestoreSwift: ba0e8ae1dca6cd395c646671a7fe2dcc077db225
853+
FirebaseInstallations: 7f1c9ae6bd9df6abe9c74124b38fa8740aba5df4
854+
FirebaseMessaging: cc9f40f5b7494680f3844d08e517e92aa4e8d9f7
829855
FirebaseSharedSwift: 6966c4de41fba13a4270de1e421e6eee2cf90113
830856
Floaty: e2bd5a0f6f7f70899e26ff2da31081c8bee8d1b0
857+
GoogleDataTransport: 1c8145da7117bd68bbbed00cf304edb6a24de00f
831858
GoogleSignIn: 5651ce3a61e56ca864160e79b484cd9ed3f49b7a
832859
GoogleUtilities: 1d20a6ad97ef46f67bbdec158ce00563a671ebb7
833860
GradientLoadingBar: 3c4246535efdedaf9b471c05caabfdb4b87b40a2
@@ -849,6 +876,6 @@ SPEC CHECKSUMS:
849876
SwiftAlgorithms: 38dda4731d19027fdeee1125f973111bf3386b53
850877
TTGSnackbar: fea91c62637b5662f808a4c3b55f7631cb7e1ef7
851878

852-
PODFILE CHECKSUM: 71ebaf0f62dbee208320c056eb7a0a0f27da3d2e
879+
PODFILE CHECKSUM: 22ee40650525751f14ffe79cbbdb8ee424a7ed85
853880

854881
COCOAPODS: 1.11.3

0 commit comments

Comments
 (0)