Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save katleta3000/6478752b49860553cdb23846992a234f to your computer and use it in GitHub Desktop.
Save katleta3000/6478752b49860553cdb23846992a234f to your computer and use it in GitHub Desktop.
iOS application push launch processing
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