From b74be29257d087812c3b0c2c239a84cdc13462d3 Mon Sep 17 00:00:00 2001 From: Laurent Date: Sun, 14 Jul 2024 14:17:08 +0200 Subject: [PATCH] APNs POC --- PadelClub.xcodeproj/project.pbxproj | 4 ++ PadelClub/AppDelegate.swift | 68 +++++++++++++++++++++++++++++ PadelClub/PadelClub.entitlements | 5 ++- PadelClub/PadelClubApp.swift | 5 +++ 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 PadelClub/AppDelegate.swift diff --git a/PadelClub.xcodeproj/project.pbxproj b/PadelClub.xcodeproj/project.pbxproj index 8b0ac36..2c21c38 100644 --- a/PadelClub.xcodeproj/project.pbxproj +++ b/PadelClub.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + C40CD2F32C412681000DBD9A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C40CD2F22C412681000DBD9A /* AppDelegate.swift */; }; C411C9C32BEBA453003017AD /* ServerDataTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C411C9C22BEBA453003017AD /* ServerDataTests.swift */; }; C411C9C92BF219CB003017AD /* UserDataTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C411C9C82BF219CB003017AD /* UserDataTests.swift */; }; C411C9D02BF38F41003017AD /* TokenExemptionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C411C9CF2BF38F41003017AD /* TokenExemptionTests.swift */; }; @@ -298,6 +299,7 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + C40CD2F22C412681000DBD9A /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; C411C9C22BEBA453003017AD /* ServerDataTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerDataTests.swift; sourceTree = ""; }; C411C9C82BF219CB003017AD /* UserDataTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserDataTests.swift; sourceTree = ""; }; C411C9CC2BF21DAF003017AD /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; @@ -660,6 +662,7 @@ FFA6D78A2BB0BEB3003A31F3 /* Info.plist */, C4EC6F562BE92CAC000CEAB4 /* local.plist */, C425D4002B6D249D002A7B48 /* PadelClubApp.swift */, + C40CD2F22C412681000DBD9A /* AppDelegate.swift */, C45BAE3A2BC6DF10002EEC8A /* SyncedProducts.storekit */, FFD784002B91BF79000F62A6 /* Launch Screen.storyboard */, C4A47D722B72881500ADC637 /* Views */, @@ -1571,6 +1574,7 @@ FFCFC00E2BBC3D4600B82851 /* PointSelectionView.swift in Sources */, FF089EB62BB00A3800F0AEC7 /* TeamRowView.swift in Sources */, FF92680B2BCEE3E10080F940 /* ContactManager.swift in Sources */, + C40CD2F32C412681000DBD9A /* AppDelegate.swift in Sources */, C49EF0262BD80AE80077B5AA /* SubscriptionInfoView.swift in Sources */, FFCFC00C2BBC3D1E00B82851 /* EditScoreView.swift in Sources */, FF7091622B90F04300AB08DA /* TournamentOrganizerView.swift in Sources */, diff --git a/PadelClub/AppDelegate.swift b/PadelClub/AppDelegate.swift new file mode 100644 index 0000000..14c46c1 --- /dev/null +++ b/PadelClub/AppDelegate.swift @@ -0,0 +1,68 @@ +// +// AppDelegate.swift +// PadelClub +// +// Created by Laurent Morvillier on 12/07/2024. +// + +import Foundation +import UIKit +import LeStorage +import UserNotifications + +class AppDelegate : NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate { + + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Bool { + // Override point for customization after application launch. + UIApplication.shared.registerForRemoteNotifications() + UNUserNotificationCenter.current().delegate = self + + Logger.log("didFinishLaunchingWithOptions") + return true + } + + func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { + + if StoreCenter.main.hasToken() { + Task { + do { + let services = try StoreCenter.main.service() + try await services.postDeviceToken(deviceToken: deviceToken) + } catch { + Logger.error(error) + } + } + } +// Logger.log("token = \(deviceToken)") + } + + func application(_ application: UIApplication, + didFailToRegisterForRemoteNotificationsWithError + error: Error) { + Logger.error(error) + } + + // MARK: - UNUserNotificationCenterDelegate + + func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { + // Show the notification as a banner even when the app is in the foreground + completionHandler([.banner, .sound, .badge]) + } + + // Handle notifications when the user taps on them + func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { + let userInfo = response.notification.request.content.userInfo + // Handle the notification content here + print("User Info: \(userInfo)") + + // Perform your custom actions based on the notification content + completionHandler() + } + + static func askPermissions() { + UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { success, error in + print("requestAuthorization > success = \(success), error = \(String(describing: error))") + } + } + +} diff --git a/PadelClub/PadelClub.entitlements b/PadelClub/PadelClub.entitlements index 0c67376..903def2 100644 --- a/PadelClub/PadelClub.entitlements +++ b/PadelClub/PadelClub.entitlements @@ -1,5 +1,8 @@ - + + aps-environment + development + diff --git a/PadelClub/PadelClubApp.swift b/PadelClub/PadelClubApp.swift index 37b0a66..7ffdf07 100644 --- a/PadelClub/PadelClubApp.swift +++ b/PadelClub/PadelClubApp.swift @@ -17,6 +17,8 @@ struct PadelClubApp: App { @StateObject var dataStore = DataStore.shared @State private var registrationError: RegistrationError? = nil + @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate + var presentError: Binding { Binding { registrationError != nil @@ -137,6 +139,9 @@ struct PadelClubApp: App { let docURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] Logger.log("doc dir = \(docURL.absoluteString)") UserDefaults.standard.set(false, forKey: "_UIConstraintBasedLayoutLogUnsatisfiable") + +// AppDelegate.askPermissions() + }