-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppDelegate.swift
94 lines (81 loc) · 4.79 KB
/
AppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// AppDelegate.swift
// SalesSparrow
//
// Created by Mohit Charkha on 31/07/23.
//
import UIKit
import UserNotifications
import FirebaseCore
import FirebaseCrashlytics
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
// In this method, you can perform any setup tasks that need to be done before the app is ready to use.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
bootService()
let uuid = UIDevice.current.identifierForVendor?.uuidString
let userId = uuid
Crashlytics.crashlytics().setUserID(userId)
// TODO: Uncomment to register for push notification
// registerForRemoteNotifications()
return true
}
// A function to set env variable in case of test running. Also fetch logged in user.
func bootService() {
let isRunningUITests = ProcessInfo.processInfo.arguments.contains("isRunningUITests")
if isRunningUITests && ProcessInfo.processInfo.arguments.count > 2 {
let launchArgs = ProcessInfo.processInfo.arguments
let testArgs: ArraySlice<String> = launchArgs[2...(launchArgs.count-1)]
let testCaseIdentifiers: [String] = [] + testArgs
Environments.shared.testVars["testCaseIdentifiers"] = testCaseIdentifiers
}
DependencyContainer.shared.setApiService(isRunningUITests: isRunningUITests)
UserStateViewModel.shared.getCurrentUser()
}
/**
Registers the app for remote notifications with Apple Push Notification service (APNs).
If permission is granted, it registers the app for remote notifications with `UIApplication.shared.registerForRemoteNotifications()`.
If there is an error requesting authorization, it prints an error message to the console.
*/
func registerForRemoteNotifications() {
UNUserNotificationCenter.current().delegate = self
// Request permission to display alerts, play sounds, and badge the app's icon
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
} else if let error = error {
print("Failed to request notification authorization: \(error)")
}
}
}
// This method is called when the app successfully registers for remote notifications. In this method, you can send the device token to your server for further processing.
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenString = deviceToken.reduce("", { $0 + String(format: "%02x", $1) })
sendDeviceTokenToServer(tokenString)
}
// This method is called when the app fails to register for remote notifications. In this method, you can handle the error and take appropriate action.
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register for remote notifications: \(error)")
}
// This method is a helper method that sends the device token to your server for further processing.
func sendDeviceTokenToServer(_ token: String) {
// Send the device token to your server for further processing
print("Device token: \(token)")
}
// This method is called when the app receives a remote notification while it is running in the foreground. In this method, you can present a local notification to the user to alert them of the incoming notification.
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// Customize the presentation options based on your requirements
completionHandler([.banner, .sound, .badge, .list])
}
// This method is called when the user taps on a remote notification. In this method, you can present a local notification to the user to alert them of the incoming notification.
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// Handle the notification payload here
let payload = response.notification.request.content
print(payload)
// Process the payload as needed
// Call the completion handler when you're done handling the notification
completionHandler()
}
}