Created
July 27, 2021 09:01
-
-
Save bergusman/aee22cbcd613f612d4efe348727b1973 to your computer and use it in GitHub Desktop.
Push Settings
This file contains 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
// | |
// Models.swift | |
// Vitaly Berg | |
// | |
// Created by Vitaly Berg on 7/21/21. | |
// Copyright © 2021 Vitaly Berg. All rights reserved. | |
// | |
import Foundation | |
import UserNotifications | |
struct PushSettings: Codable { | |
enum AuthorizationStatus: String, Codable { | |
case notDetermined | |
case denied | |
case authorized | |
case provisional | |
case ephemeral | |
case unknown | |
} | |
enum ShowPreviewsSetting: String, Codable { | |
case always | |
case whenAuthenticated | |
case never | |
case unknown | |
} | |
enum Setting: String, Codable { | |
case notSupported | |
case disabled | |
case enabled | |
case unknown | |
} | |
enum AlertStyle: String, Codable { | |
case none | |
case banner | |
case alert | |
case unknown | |
} | |
let authorizationStatus: AuthorizationStatus | |
let soundSetting: Setting | |
let badgeSetting: Setting | |
let alertSetting: Setting | |
let notificationCenterSetting: Setting | |
let lockScreenSetting: Setting | |
let carPlaySetting: Setting | |
let alertStyle: AlertStyle | |
let showPreviewsSetting: ShowPreviewsSetting | |
let criticalAlertSetting: Setting | |
let providesAppNotificationSettings: Bool | |
let announcementSetting: Setting | |
} | |
extension PushSettings { | |
init(_ settings: UNNotificationSettings) { | |
func takeAuthorizationStatus(_ status: UNAuthorizationStatus) -> AuthorizationStatus { | |
switch status { | |
case .authorized: return .authorized | |
case .denied: return .denied | |
case .notDetermined: return .notDetermined | |
case .provisional: return .provisional | |
case .ephemeral: return .ephemeral | |
@unknown default: return .unknown | |
} | |
} | |
func takeShowPreviewsSetting(_ setting: UNShowPreviewsSetting) -> ShowPreviewsSetting { | |
switch setting { | |
case .always: return .always | |
case .never: return .never | |
case .whenAuthenticated: return .whenAuthenticated | |
@unknown default: return .unknown | |
} | |
} | |
func takeSetting(_ setting: UNNotificationSetting) -> Setting { | |
switch setting { | |
case .disabled: return .disabled | |
case .enabled: return .enabled | |
case .notSupported: return .notSupported | |
@unknown default: return .unknown | |
} | |
} | |
func takeAlertStyle(_ style: UNAlertStyle) -> AlertStyle { | |
switch style { | |
case .alert: return .alert | |
case .banner: return .banner | |
case .none: return .none | |
@unknown default: return .unknown | |
} | |
} | |
authorizationStatus = takeAuthorizationStatus(settings.authorizationStatus) | |
soundSetting = takeSetting(settings.soundSetting) | |
badgeSetting = takeSetting(settings.badgeSetting) | |
alertSetting = takeSetting(settings.alertSetting) | |
notificationCenterSetting = takeSetting(settings.notificationCenterSetting) | |
lockScreenSetting = takeSetting(settings.lockScreenSetting) | |
carPlaySetting = takeSetting(settings.carPlaySetting) | |
alertStyle = takeAlertStyle(settings.alertStyle) | |
showPreviewsSetting = takeShowPreviewsSetting(settings.showPreviewsSetting) | |
criticalAlertSetting = takeSetting(settings.criticalAlertSetting) | |
providesAppNotificationSettings = settings.providesAppNotificationSettings | |
announcementSetting = takeSetting(settings.announcementSetting) | |
} | |
} |
This file contains 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
UNUserNotificationCenter.current().getNotificationSettings { (settings) in | |
let pushSettings = PushSettings(settings) | |
// Send to server with device token | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment