Created
July 30, 2023 19:34
-
-
Save katleta3000/6478752b49860553cdb23846992a234f to your computer and use it in GitHub Desktop.
iOS application push launch processing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import UserNotifications | |
final class SceneDelegate: UIResponder, UIWindowSceneDelegate { | |
private let pushAppLaunchRule = PushAppLaunchRule() | |
var window: UIWindow? | |
func sceneDidBecomeActive(_ scene: UIScene) { | |
UNUserNotificationCenter.current().delegate = self | |
} | |
func scene( | |
_ scene: UIScene, | |
willConnectTo session: UISceneSession, | |
options connectionOptions: UIScene.ConnectionOptions) { | |
// Process push from unattached or suspended states | |
guard let notificationResponse = connectionOptions.notificationResponse else { return } | |
pushAppLaunchRule.perform(with: notificationResponse) | |
} | |
} | |
extension SceneDelegate: UNUserNotificationCenterDelegate { | |
public func userNotificationCenter( | |
_ center: UNUserNotificationCenter, | |
willPresent notification: UNNotification, | |
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { | |
completionHandler([.banner, .sound, .list]) | |
} | |
public func userNotificationCenter( | |
_ center: UNUserNotificationCenter, | |
didReceive response: UNNotificationResponse, | |
withCompletionHandler completionHandler: @escaping () -> Void) { | |
// Process push from background state | |
pushAppLaunchRule.perform(with: response) | |
} | |
} | |
struct PushAppLaunchRule { | |
func perform(with response: UNNotificationResponse) { | |
if response.notification.request.trigger is UNTimeIntervalNotificationTrigger { | |
// process your local push | |
} else if response.notification.request.trigger is UNCalendarNotificationTrigger { | |
// process your local push | |
} else if response.notification.request.trigger is UNPushNotificationTrigger { | |
// process your remote push | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment