-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhereaboutsApp.swift
114 lines (92 loc) · 3.8 KB
/
whereaboutsApp.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// whereaboutsApp.swift
// whereabouts
//
// Created by Ritam Saha on 4/17/23.
//
import SwiftUI
import Firebase
import UserNotifications
import FirebaseMessaging
import NearbyInteraction
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
var isSupported: Bool
if #available(iOS 16.0, *) {
isSupported = NISession.deviceCapabilities.supportsPreciseDistanceMeasurement
} else {
isSupported = NISession.isSupported
}
// Add your existing Firebase code here
FirebaseApp.configure()
requestNotificationAuthorization(application: application)
Messaging.messaging().delegate = self
return true
}
func requestNotificationAuthorization(application: UIApplication) {
let center = UNUserNotificationCenter.current()
center.delegate = self
let options: UNAuthorizationOptions = [.alert, .badge, .sound]
center.requestAuthorization(options: options) { granted, error in
if let error = error {
print("Request authorization for notifications failed with error: \(error.localizedDescription)")
} else {
print("Notification permission granted: \(granted)")
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register for remote notifications with error: \(error.localizedDescription)")
}
}
extension AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
guard let fcmToken = fcmToken else { return }
print("Received FCM token: \(fcmToken)")
// Store or send the FCM token to your server as needed
}
}
extension AppDelegate {
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
handleRemoteNotification(userInfo: userInfo)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
handleRemoteNotification(userInfo: userInfo)
completionHandler()
}
func handleRemoteNotification(userInfo: [AnyHashable: Any]) {
print("passed")
let appViewModel = AppViewModel.shared
appViewModel.cancelButton = true
appViewModel.selectedTab = 1
if let email = userInfo["email"] as? String {
appViewModel.drawRouteToUserByEmail(email: email)
print(email)
// Handle the custom data here
}
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// Show the notification banner
completionHandler([.banner, .sound])
}
}
@main
struct WhereaboutsApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(AppViewModel.shared)
}
}
}