Notification registration improvements

This commit is contained in:
Justin Mazzocchi 2021-01-28 20:59:06 -08:00
parent 741545fc1f
commit 366ea17d04
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
3 changed files with 35 additions and 16 deletions

View file

@ -210,7 +210,9 @@ public extension IdentityDatabase {
func fetchIdentitiesWithOutdatedDeviceTokens(deviceToken: Data) -> AnyPublisher<[Identity], Error> { func fetchIdentitiesWithOutdatedDeviceTokens(deviceToken: Data) -> AnyPublisher<[Identity], Error> {
databaseWriter.readPublisher( databaseWriter.readPublisher(
value: IdentityInfo.request(IdentityRecord.order(IdentityRecord.Columns.lastUsedAt.desc)) value: IdentityInfo.request(IdentityRecord.order(IdentityRecord.Columns.lastUsedAt.desc))
.filter(IdentityRecord.Columns.lastRegisteredDeviceToken != deviceToken) .filter(IdentityRecord.Columns.authenticated == true
&& IdentityRecord.Columns.pending == false
&& IdentityRecord.Columns.lastRegisteredDeviceToken != deviceToken)
.fetchAll) .fetchAll)
.map { $0.map(Identity.init(info:)) } .map { $0.map(Identity.init(info:)) }
.eraseToAnyPublisher() .eraseToAnyPublisher()

View file

@ -5,7 +5,7 @@ import Foundation
import UserNotifications import UserNotifications
public struct UserNotificationService { public struct UserNotificationService {
let events: AnyPublisher<UserNotificationClient.DelegateEvent, Never> public let events: AnyPublisher<Event, Never>
private let userNotificationClient: UserNotificationClient private let userNotificationClient: UserNotificationClient
@ -16,12 +16,14 @@ public struct UserNotificationService {
} }
public extension UserNotificationService { public extension UserNotificationService {
func isAuthorized() -> AnyPublisher<Bool, Error> { typealias Event = UserNotificationClient.DelegateEvent
func isAuthorized(request: Bool) -> AnyPublisher<Bool, Error> {
getNotificationSettings() getNotificationSettings()
.map(\.authorizationStatus) .map(\.authorizationStatus)
.flatMap { status -> AnyPublisher<Bool, Error> in .flatMap { status -> AnyPublisher<Bool, Error> in
if status == .notDetermined { if request, status == .notDetermined {
return requestProvisionalAuthorization().eraseToAnyPublisher() return requestAuthorization().eraseToAnyPublisher()
} }
return Just(status == .authorized || status == .provisional) return Just(status == .authorized || status == .provisional)
@ -40,9 +42,9 @@ private extension UserNotificationService {
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
func requestProvisionalAuthorization() -> AnyPublisher<Bool, Error> { func requestAuthorization() -> AnyPublisher<Bool, Error> {
Future<Bool, Error> { promise in Future<Bool, Error> { promise in
userNotificationClient.requestAuthorization([.alert, .sound, .badge, .provisional]) { granted, error in userNotificationClient.requestAuthorization([.alert, .sound, .badge]) { granted, error in
if let error = error { if let error = error {
promise(.failure(error)) promise(.failure(error))
} else { } else {

View file

@ -32,13 +32,17 @@ public final class RootViewModel: ObservableObject {
.sink { [weak self] in self?.identitySelected(id: $0) } .sink { [weak self] in self?.identitySelected(id: $0) }
.store(in: &cancellables) .store(in: &cancellables)
userNotificationService.isAuthorized() userNotificationService.isAuthorized(request: false)
.filter { $0 } .filter { $0 }
.zip(registerForRemoteNotifications()) .zip(registerForRemoteNotifications())
.map { $1 } .map { $1 }
.flatMap(allIdentitiesService.updatePushSubscriptions(deviceToken:)) .flatMap(allIdentitiesService.updatePushSubscriptions(deviceToken:))
.sink { _ in } receiveValue: { _ in } .sink { _ in } receiveValue: { _ in }
.store(in: &cancellables) .store(in: &cancellables)
userNotificationService.events
.sink { [weak self] in self?.handle(event: $0) }
.store(in: &cancellables)
} }
} }
@ -112,17 +116,28 @@ private extension RootViewModel {
.sink { _ in } receiveValue: { _ in } .sink { _ in } receiveValue: { _ in }
.store(in: &self.cancellables) .store(in: &self.cancellables)
self.userNotificationService.isAuthorized() if identityContext.identity.authenticated && !identityContext.identity.pending {
.filter { $0 } self.userNotificationService.isAuthorized(request: true)
.zip(self.registerForRemoteNotifications()) .filter { $0 }
.filter { identityContext.identity.lastRegisteredDeviceToken != $1 } .zip(self.registerForRemoteNotifications())
.map { ($1, identityContext.identity.pushSubscriptionAlerts) } .filter { identityContext.identity.lastRegisteredDeviceToken != $1 }
.flatMap(identityContext.service.createPushSubscription(deviceToken:alerts:)) .map { ($1, identityContext.identity.pushSubscriptionAlerts) }
.sink { _ in } receiveValue: { _ in } .flatMap(identityContext.service.createPushSubscription(deviceToken:alerts:))
.store(in: &self.cancellables) .sink { _ in } receiveValue: { _ in }
.store(in: &self.cancellables)
}
return NavigationViewModel(identityContext: identityContext) return NavigationViewModel(identityContext: identityContext)
} }
.assign(to: &$navigationViewModel) .assign(to: &$navigationViewModel)
} }
func handle(event: UserNotificationService.Event) {
switch event {
case let .willPresentNotification(_, completionHandler):
completionHandler(.banner)
default:
break
}
}
} }