-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathNotificationSettingsServiceRemote.swift
More file actions
131 lines (117 loc) · 5.29 KB
/
NotificationSettingsServiceRemote.swift
File metadata and controls
131 lines (117 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import Foundation
import WordPressKitObjC
/// The purpose of this class is to encapsulate all of the interaction with the Notifications REST endpoints.
/// Here we'll deal mostly with the Settings / Push Notifications API.
///
open class NotificationSettingsServiceRemote: ServiceRemoteWordPressComREST {
/// Retrieves all of the Notification Settings
///
/// - Parameters:
/// - deviceId: The ID of the current device. Can be nil.
/// - success: A closure to be called on success, which will receive the parsed settings entities.
/// - failure: Optional closure to be called on failure. Will receive the error that was encountered.
///
open func getAllSettings(_ deviceId: String, success: (([RemoteNotificationSettings]) -> Void)?, failure: ((NSError?) -> Void)?) {
let path = String(format: "me/notifications/settings/?device_id=%@", deviceId)
let requestUrl = self.path(forEndpoint: path, withVersion: ._1_1)
wordPressComRESTAPI.get(requestUrl,
parameters: nil,
success: { response, _ in
let settings = RemoteNotificationSettings.fromDictionary(response as? NSDictionary)
success?(settings)
},
failure: { error, _ in
failure?(error as NSError)
})
}
/// Updates the specified Notification Settings
///
/// - Parameters:
/// - settings: The complete (or partial) dictionary of settings to be updated.
/// - success: Optional closure to be called on success.
/// - failure: Optional closure to be called on failure.
///
@objc open func updateSettings(_ settings: [String: AnyObject], success: (() -> Void)?, failure: ((NSError?) -> Void)?) {
let path = String(format: "me/notifications/settings/")
let requestUrl = self.path(forEndpoint: path, withVersion: ._1_1)
let parameters = settings
wordPressComRESTAPI.post(requestUrl,
parameters: parameters,
success: { _, _ in
success?()
},
failure: { error, _ in
failure?(error as NSError)
})
}
/// Registers a given Apple Push Token in the WordPress.com Backend.
///
/// - Parameters:
/// - token: The token of the device to be registered.
/// - pushNotificationAppId: The app id to be registered.
/// - deviceInformation: Metadata about the device being registered.
/// - success: Optional closure to be called on success.
/// - failure: Optional closure to be called on failure.
///
open func registerDeviceForPushNotifications(_ token: String, pushNotificationAppId: String, deviceInformation: some DeviceInformationProvider, success: ((_ deviceId: String) -> Void)?, failure: ((NSError) -> Void)?) {
let endpoint = "devices/new"
let requestUrl = path(forEndpoint: endpoint, withVersion: ._1_1)
let parameters = [
"device_token": token,
"device_family": "apple",
"app_secret_key": pushNotificationAppId,
"device_name": deviceInformation.name,
"device_model": deviceInformation.hardwarePlatform,
"os_version": deviceInformation.systemVersion,
"app_version": Bundle.main.wpkit_bundleVersion(),
"device_uuid": deviceInformation.identifierForVendor?.uuidString
]
wordPressComRESTAPI.post(requestUrl,
parameters: parameters as [String: Any],
success: { response, _ in
if let responseDict = response as? NSDictionary,
let rawDeviceId = responseDict.object(forKey: "ID") {
// Failsafe: Make sure deviceId is always a string
let deviceId = String(format: "\(rawDeviceId)")
success?(deviceId)
} else {
let innerError = Error.invalidResponse
let outerError = NSError(domain: innerError.domain, code: innerError.code, userInfo: nil)
failure?(outerError)
}
},
failure: { error, _ in
failure?(error as NSError)
})
}
/// Unregisters a given DeviceID for Push Notifications
///
/// - Parameters:
/// - deviceId: The ID of the device to be unregistered.
/// - success: Optional closure to be called on success.
/// - failure: Optional closure to be called on failure.
///
@objc open func unregisterDeviceForPushNotifications(_ deviceId: String, success: (() -> Void)?, failure: ((NSError) -> Void)?) {
let endpoint = String(format: "devices/%@/delete", deviceId)
let requestUrl = path(forEndpoint: endpoint, withVersion: ._1_1)
wordPressComRESTAPI.post(requestUrl,
parameters: nil,
success: { _, _ in
success?()
},
failure: { error, _ in
failure?(error as NSError)
})
}
/// Describes all of the possible errors that might be generated by this class.
///
public enum Error: Int {
case invalidResponse = -1
var code: Int {
return rawValue
}
var domain: String {
return "NotificationSettingsServiceRemote"
}
}
}