diff --git a/Sources/AllInApp/AllIn/AllInApp.swift b/Sources/AllInApp/AllIn/AllInApp.swift index c4209b3..22cb3b1 100644 --- a/Sources/AllInApp/AllIn/AllInApp.swift +++ b/Sources/AllInApp/AllIn/AllInApp.swift @@ -23,6 +23,9 @@ struct AllInApp: App { var body: some Scene { WindowGroup { ContentView() + .onAppear { + AppStateContainer.shared.notificationState.scheduleNotifications() + } .onChange(of: phase) { newPhase in switch newPhase { case .background, .inactive: diff --git a/Sources/AllInApp/AllIn/AppState.swift b/Sources/AllInApp/AllIn/AppState.swift index ca9d3f4..a276067 100644 --- a/Sources/AllInApp/AllIn/AppState.swift +++ b/Sources/AllInApp/AllIn/AppState.swift @@ -12,6 +12,7 @@ class AppStateContainer: ObservableObject { static let shared = AppStateContainer() let loggedState: LoggedState = LoggedState() var onlineStatus: OnlineStatus = OnlineStatus() + var notificationState: NotificationService = NotificationService() @Published var user: User? @AppStorage("authenticationRefresh") var authenticationRefresh: String? diff --git a/Sources/AllInApp/AllIn/QuickActions/Delegates.swift b/Sources/AllInApp/AllIn/QuickActions/Delegates.swift index ef1c14b..fc16726 100644 --- a/Sources/AllInApp/AllIn/QuickActions/Delegates.swift +++ b/Sources/AllInApp/AllIn/QuickActions/Delegates.swift @@ -2,7 +2,7 @@ // Delegates.swift // AllIn // -// Created by Emre KARTAL on 22/02/2024. +// Created by Emre on 22/02/2024. // import UIKit diff --git a/Sources/AllInApp/AllIn/QuickActions/QuickAction.swift b/Sources/AllInApp/AllIn/QuickActions/QuickAction.swift index 1f03f5a..508f9dc 100644 --- a/Sources/AllInApp/AllIn/QuickActions/QuickAction.swift +++ b/Sources/AllInApp/AllIn/QuickActions/QuickAction.swift @@ -2,7 +2,7 @@ // QuickAction.swift // AllIn // -// Created by étudiant on 22/02/2024. +// Created by Emre on 22/02/2024. // import UIKit diff --git a/Sources/AllInApp/AllIn/Services/NotificationService.swift b/Sources/AllInApp/AllIn/Services/NotificationService.swift new file mode 100644 index 0000000..d768421 --- /dev/null +++ b/Sources/AllInApp/AllIn/Services/NotificationService.swift @@ -0,0 +1,50 @@ +// +// NotificationService.swift +// AllIn +// +// Created by Emre on 22/02/2024. +// + +import UserNotifications + +struct NotificationItem { + var title: String + var content: String + var interval: TimeInterval +} + +class NotificationService: ObservableObject { + + @Published var notifications: [NotificationItem] = [] + + func scheduleNotifications() { + UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in + if granted { + print("Permission for notifications granted.") + } else { + print("Permission for notifications denied.") + } + } + } + + func loadNotifications() { + for notification in notifications { + scheduleNotification(with: notification) + } + } + + func removeAllNotifications() { + UNUserNotificationCenter.current().removeAllPendingNotificationRequests() + } + + func scheduleNotification(with item: NotificationItem) { + let content = UNMutableNotificationContent() + content.title = item.title + content.body = item.content + + let trigger = UNTimeIntervalNotificationTrigger(timeInterval: item.interval, repeats: false) + let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger) + + UNUserNotificationCenter.current().add(request) + } +} diff --git a/Sources/AllInApp/AllInApp.xcodeproj/project.pbxproj b/Sources/AllInApp/AllInApp.xcodeproj/project.pbxproj index cd0a5d7..59d7330 100644 --- a/Sources/AllInApp/AllInApp.xcodeproj/project.pbxproj +++ b/Sources/AllInApp/AllInApp.xcodeproj/project.pbxproj @@ -64,6 +64,7 @@ EC7A882F2B28E6BE004F226A /* ConfidentialityButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC7A882E2B28E6BE004F226A /* ConfidentialityButton.swift */; }; EC7EF7482B87E3E00022B5D9 /* Delegates.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC7EF7472B87E3E00022B5D9 /* Delegates.swift */; }; EC7EF74A2B87E3FD0022B5D9 /* QuickAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC7EF7492B87E3FD0022B5D9 /* QuickAction.swift */; }; + EC7EF74C2B87F2AF0022B5D9 /* NotificationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC7EF74B2B87F2AF0022B5D9 /* NotificationService.swift */; }; EC89F7BD2B250D66003821CE /* LoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC89F7BC2B250D66003821CE /* LoginView.swift */; }; EC9464E92B7413E1004EEBD8 /* BetEndingValidationViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC9464E82B7413E1004EEBD8 /* BetEndingValidationViewModel.swift */; }; ECA9D1C92B2D9ADA0076E0EC /* UserInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECA9D1C82B2D9ADA0076E0EC /* UserInfo.swift */; }; @@ -172,6 +173,7 @@ EC7A882E2B28E6BE004F226A /* ConfidentialityButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfidentialityButton.swift; sourceTree = ""; }; EC7EF7472B87E3E00022B5D9 /* Delegates.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Delegates.swift; sourceTree = ""; }; EC7EF7492B87E3FD0022B5D9 /* QuickAction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuickAction.swift; sourceTree = ""; }; + EC7EF74B2B87F2AF0022B5D9 /* NotificationService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationService.swift; sourceTree = ""; }; EC89F7BC2B250D66003821CE /* LoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginView.swift; sourceTree = ""; }; EC9464E82B7413E1004EEBD8 /* BetEndingValidationViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BetEndingValidationViewModel.swift; sourceTree = ""; }; ECA9D1C82B2D9ADA0076E0EC /* UserInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserInfo.swift; sourceTree = ""; }; @@ -295,6 +297,7 @@ children = ( EC1D15402B715A7A0094833E /* Protocols */, EC6B96D02B24BAE800FC1C58 /* AuthService.swift */, + EC7EF74B2B87F2AF0022B5D9 /* NotificationService.swift */, ); path = Services; sourceTree = ""; @@ -585,6 +588,7 @@ 129D051D2B6E7FF0003D3E08 /* OddCapsule.swift in Sources */, 123225D92B67B46100D30BB3 /* BetEndingValidationView.swift in Sources */, EC0193782B25BF16005D81E6 /* AllcoinsCapsule.swift in Sources */, + EC7EF74C2B87F2AF0022B5D9 /* NotificationService.swift in Sources */, EC650A4A2B25DD58003AFCAD /* FriendsView.swift in Sources */, EC3077072B24CB840060E34D /* SplashView.swift in Sources */, EC01937E2B25C52E005D81E6 /* TopBar.swift in Sources */,