metatext-app-ios-iphone-ipad/System/AppDelegate.swift

43 lines
1.4 KiB
Swift
Raw Normal View History

2020-08-12 09:24:39 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
import UIKit
import Combine
class AppDelegate: NSObject {
2020-08-27 06:25:28 +02:00
@Published private var application: UIApplication?
2020-08-12 09:24:39 +02:00
private let remoteNotificationDeviceTokens = PassthroughSubject<Data, Error>()
}
extension AppDelegate {
func registerForRemoteNotifications() -> AnyPublisher<String, Error> {
$application
.compactMap { $0 }
.handleEvents(receiveOutput: { $0.registerForRemoteNotifications() })
.setFailureType(to: Error.self)
.zip(remoteNotificationDeviceTokens)
.first()
.map { $1.hexEncodedString() }
.eraseToAnyPublisher()
}
}
2020-08-27 06:25:28 +02:00
extension AppDelegate: UIApplicationDelegate {
2020-08-12 09:24:39 +02:00
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
self.application = application
return true
}
2020-08-27 06:25:28 +02:00
func application(_ application: UIApplication,
2020-08-12 09:24:39 +02:00
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
remoteNotificationDeviceTokens.send(deviceToken)
}
2020-08-27 06:25:28 +02:00
func application(_ application: UIApplication,
2020-08-12 09:24:39 +02:00
didFailToRegisterForRemoteNotificationsWithError error: Error) {
remoteNotificationDeviceTokens.send(completion: .failure(error))
}
}