Skip to content

Commit

Permalink
Added a device identifier data.
Browse files Browse the repository at this point in the history
  • Loading branch information
buh committed Sep 9, 2019
1 parent c2272eb commit a40e50b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
16 changes: 14 additions & 2 deletions Sources/Core/Client/Client+Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,24 @@ public extension Client {

public extension Client {

/// Add a device for Push Notifications.
///
/// - Parameter deviceData: a Push Notifications device identifier data.
/// - Returns: an observable completion.
func addDevice(deviceData: Data) -> Observable<EmptyData> {
guard !deviceData.isEmpty, let deviceId = String(data: deviceData, encoding: .utf8) else {
return .empty()
}

return addDevice(deviceId: deviceId)
}

/// Add a device for Push Notifications.
///
/// - Parameter deviceId: a Push Notifications device identifier.
/// - Returns: an observable empty data.
/// - Returns: an observable completion.
func addDevice(deviceId: String) -> Observable<EmptyData> {
guard let user = User.current else {
guard let user = User.current, !user.devices.contains(where: { $0.id == deviceId }) else {
return .empty()
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/Core/Notifications/Notifications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public final class Notifications: NSObject {

var logger: ClientLogger?

var logsEnabled: Bool = false {
/// Enable logs for Notifications.
public var logsEnabled: Bool = false {
didSet {
logger = logsEnabled ? ClientLogger(icon: "🗞") : nil
}
Expand Down Expand Up @@ -75,15 +76,14 @@ public final class Notifications: NSObject {
self.logger?.log("👍 Notifications authorized (\(settings.authorizationStatus.rawValue))")
}
}

}

/// Ask permissions to make notifications work.
public func askForPermissions() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { didAllow, error in
if didAllow {
self.authorizationStatus = .authorized
self.logger?.log("👍 User has accepter notifications")
self.logger?.log("👍 User has accepted notifications")
} else if let error = error {
self.logger?.log("❌ User has declined notifications \(error)")
} else {
Expand Down

0 comments on commit a40e50b

Please sign in to comment.