|
| 1 | +/** |
| 2 | + * Modified MIT License |
| 3 | + * |
| 4 | + * Copyright 2024 OneSignal |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * 1. The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * 2. All copies of substantial portions of the Software may only be used in connection |
| 17 | + * with services provided by OneSignal. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +import SwiftUI |
| 29 | +import OneSignalFramework |
| 30 | +import OneSignalInAppMessages |
| 31 | +import OneSignalLocation |
| 32 | + |
| 33 | +@main |
| 34 | +struct OneSignalSwiftUIExampleApp: App { |
| 35 | + @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate |
| 36 | + @StateObject private var viewModel = OneSignalViewModel() |
| 37 | + |
| 38 | + var body: some Scene { |
| 39 | + WindowGroup { |
| 40 | + ContentView() |
| 41 | + .environmentObject(viewModel) |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// MARK: - App Delegate |
| 47 | + |
| 48 | +class AppDelegate: NSObject, UIApplicationDelegate { |
| 49 | + |
| 50 | + func application( |
| 51 | + _ application: UIApplication, |
| 52 | + didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil |
| 53 | + ) -> Bool { |
| 54 | + // Initialize OneSignal |
| 55 | + OneSignalService.shared.initialize(launchOptions: launchOptions) |
| 56 | + |
| 57 | + // Set up notification lifecycle listeners |
| 58 | + setupNotificationListeners() |
| 59 | + |
| 60 | + // Set up in-app message listeners |
| 61 | + setupInAppMessageListeners() |
| 62 | + |
| 63 | + return true |
| 64 | + } |
| 65 | + |
| 66 | + private func setupNotificationListeners() { |
| 67 | + // Foreground notification display |
| 68 | + OneSignal.Notifications.addForegroundLifecycleListener(NotificationLifecycleHandler.shared) |
| 69 | + |
| 70 | + // Notification click handling |
| 71 | + OneSignal.Notifications.addClickListener(NotificationClickHandler.shared) |
| 72 | + } |
| 73 | + |
| 74 | + private func setupInAppMessageListeners() { |
| 75 | + // In-app message lifecycle |
| 76 | + OneSignal.InAppMessages.addLifecycleListener(InAppMessageLifecycleHandler.shared) |
| 77 | + |
| 78 | + // In-app message click handling |
| 79 | + OneSignal.InAppMessages.addClickListener(InAppMessageClickHandler.shared) |
| 80 | + |
| 81 | + // Start with IAM paused |
| 82 | + OneSignal.InAppMessages.paused = true |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// MARK: - Notification Handlers |
| 87 | + |
| 88 | +class NotificationLifecycleHandler: NSObject, OSNotificationLifecycleListener { |
| 89 | + static let shared = NotificationLifecycleHandler() |
| 90 | + |
| 91 | + func onWillDisplay(event: OSNotificationWillDisplayEvent) { |
| 92 | + print("[OneSignal] Notification will display: \(event.notification.title ?? "No title")") |
| 93 | + // Optionally modify display behavior |
| 94 | + // event.preventDefault() // Prevent automatic display |
| 95 | + // event.notification.display() // Manually display later |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +class NotificationClickHandler: NSObject, OSNotificationClickListener { |
| 100 | + static let shared = NotificationClickHandler() |
| 101 | + |
| 102 | + func onClick(event: OSNotificationClickEvent) { |
| 103 | + print("[OneSignal] Notification clicked: \(event.notification.title ?? "No title")") |
| 104 | + // Handle notification click - navigate to specific screen, etc. |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// MARK: - In-App Message Handlers |
| 109 | + |
| 110 | +class InAppMessageLifecycleHandler: NSObject, OSInAppMessageLifecycleListener { |
| 111 | + static let shared = InAppMessageLifecycleHandler() |
| 112 | + |
| 113 | + func onWillDisplay(event: OSInAppMessageWillDisplayEvent) { |
| 114 | + print("[OneSignal] IAM will display: \(event.message.messageId)") |
| 115 | + } |
| 116 | + |
| 117 | + func onDidDisplay(event: OSInAppMessageDidDisplayEvent) { |
| 118 | + print("[OneSignal] IAM did display: \(event.message.messageId)") |
| 119 | + } |
| 120 | + |
| 121 | + func onWillDismiss(event: OSInAppMessageWillDismissEvent) { |
| 122 | + print("[OneSignal] IAM will dismiss: \(event.message.messageId)") |
| 123 | + } |
| 124 | + |
| 125 | + func onDidDismiss(event: OSInAppMessageDidDismissEvent) { |
| 126 | + print("[OneSignal] IAM did dismiss: \(event.message.messageId)") |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +class InAppMessageClickHandler: NSObject, OSInAppMessageClickListener { |
| 131 | + static let shared = InAppMessageClickHandler() |
| 132 | + |
| 133 | + func onClick(event: OSInAppMessageClickEvent) { |
| 134 | + print("[OneSignal] IAM clicked: \(event.result.actionId ?? "No action ID")") |
| 135 | + // Handle IAM click - navigate, track event, etc. |
| 136 | + } |
| 137 | +} |
0 commit comments