Skip to content

Commit 22020fc

Browse files
authored
Merge pull request #356 from zapcannon87/developer
feat(push): support extra parameters
2 parents e2d4bb1 + ddbbd57 commit 22020fc

File tree

2 files changed

+76
-45
lines changed

2 files changed

+76
-45
lines changed

LeanCloudTests/LCPushTestCase.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,11 @@ class LCPushTestCase: BaseTestCase {
5656
XCTAssertTrue(LCPush.send(data: data, expirationInterval: 5).isSuccess)
5757
}
5858

59+
func testExtraParameters() {
60+
let data = ["alert": "test"]
61+
62+
let extraParameters = ["apns_team_id": "LeanCloud"]
63+
64+
XCTAssertTrue(LCPush.send(data: data, extraParameters: extraParameters).isSuccess)
65+
}
5966
}

Sources/Foundation/PushClient.swift

Lines changed: 69 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,60 @@
88

99
import Foundation
1010

11+
/// LeanCloud Push Client
1112
public class LCPush {
1213

13-
/// send push notification synchronously
14-
///
14+
/// Send push notification synchronously.
1515
/// - Parameters:
16-
/// - application: The application
17-
/// - data: The data of the push message
18-
/// - query: The query condition, if this parameter be set, then `channels` will be ignored
19-
/// - channels: The channels condition, if `query` be set, then this parameter will be ignored
20-
/// - pushDate: The date of sending
21-
/// - expirationDate: The date of expiration
22-
/// - expirationInterval: If time interval since sending date is greater then this parameter, then the push expired
23-
/// - Returns: Result
16+
/// - application: The application, default is `LCApplication.default`.
17+
/// - data: The body data of the push message.
18+
/// - query: The query condition, if this parameter be set, then `channels` will be ignored.
19+
/// - channels: The channels condition, if `query` be set, then this parameter will be ignored.
20+
/// - pushDate: The date when to send.
21+
/// - expirationDate: The expiration date of this push notification.
22+
/// - expirationInterval: The expiration interval from `pushDate` of this push notification.
23+
/// - extraParameters: The extra parameters, for some specific configuration.
24+
/// - Returns: Boolean result, see `LCBooleanResult`.
2425
public static func send(
25-
application: LCApplication = LCApplication.default,
26+
application: LCApplication = .default,
2627
data: [String: Any],
2728
query: LCQuery? = nil,
2829
channels: [String]? = nil,
2930
pushDate: Date? = nil,
3031
expirationDate: Date? = nil,
31-
expirationInterval: TimeInterval? = nil)
32+
expirationInterval: TimeInterval? = nil,
33+
extraParameters: [String: Any]? = nil)
3234
-> LCBooleanResult
3335
{
3436
return expect { (fulfill) in
35-
self.send(application: application, data: data, query: query, channels: channels, pushDate: pushDate, expirationDate: expirationDate, expirationInterval: expirationInterval, completionInBackground: { (result) in
36-
fulfill(result)
37+
self.send(
38+
application: application,
39+
data: data,
40+
query: query,
41+
channels: channels,
42+
pushDate: pushDate,
43+
expirationDate: expirationDate,
44+
expirationInterval: expirationInterval,
45+
extraParameters: extraParameters,
46+
completionInBackground: { (result) in
47+
fulfill(result)
3748
})
3849
}
3950
}
4051

41-
/// send push notification asynchronously
42-
///
52+
/// Send push notification asynchronously.
4353
/// - Parameters:
44-
/// - application: The application
45-
/// - data: The data of the push message
46-
/// - query: The query condition, if this parameter be set, then `channels` will be ignored
47-
/// - channels: The channels condition, if `query` be set, then this parameter will be ignored
48-
/// - pushDate: The date of sending
49-
/// - expirationDate: The date of expiration
50-
/// - expirationInterval: If time interval since sending date is greater then this parameter, then the push expired
51-
/// - completion: The callback of the result
52-
/// - Returns: Request
54+
/// - application: The application, default is `LCApplication.default`.
55+
/// - data: The body data of the push message.
56+
/// - query: The query condition, if this parameter be set, then `channels` will be ignored.
57+
/// - channels: The channels condition, if `query` be set, then this parameter will be ignored.
58+
/// - pushDate: The date when to send.
59+
/// - expirationDate: The expiration date of this push notification.
60+
/// - expirationInterval: The expiration interval from `pushDate` of this push notification.
61+
/// - extraParameters: The extra parameters, for some specific configuration.
62+
/// - completionQueue: The queue where `completion` be called.
63+
/// - completion: The result callback.
64+
/// - Returns: The request, see `LCRequest`.
5365
@discardableResult
5466
public static func send(
5567
application: LCApplication = .default,
@@ -59,14 +71,24 @@ public class LCPush {
5971
pushDate: Date? = nil,
6072
expirationDate: Date? = nil,
6173
expirationInterval: TimeInterval? = nil,
74+
extraParameters: [String: Any]? = nil,
6275
completionQueue: DispatchQueue = .main,
6376
completion: @escaping (LCBooleanResult) -> Void)
6477
-> LCRequest
6578
{
66-
return self.send(application: application, data: data, query: query, channels: channels, pushDate: pushDate, expirationDate: expirationDate, expirationInterval: expirationInterval, completionInBackground: { (result) in
67-
completionQueue.async {
68-
completion(result)
69-
}
79+
return self.send(
80+
application: application,
81+
data: data,
82+
query: query,
83+
channels: channels,
84+
pushDate: pushDate,
85+
expirationDate: expirationDate,
86+
expirationInterval: expirationInterval,
87+
extraParameters: extraParameters,
88+
completionInBackground: { (result) in
89+
completionQueue.async {
90+
completion(result)
91+
}
7092
})
7193
}
7294

@@ -79,38 +101,40 @@ public class LCPush {
79101
pushDate: Date?,
80102
expirationDate: Date?,
81103
expirationInterval: TimeInterval?,
104+
extraParameters: [String: Any]?,
82105
completionInBackground completion: @escaping (LCBooleanResult) -> Void)
83106
-> LCRequest
84107
{
85-
let httpClient: HTTPClient = application.httpClient
86-
87108
var parameters: [String: Any] = [
88109
"prod": application.pushMode,
89-
"data": data
110+
"data": data,
90111
]
91-
if let query: LCQuery = query {
92-
parameters.merge(query.lconValue) { (current, _) in current }
93-
} else if let channels: [String] = channels {
112+
if let query = query {
113+
if let whereCondition = query.lconValue["where"] {
114+
parameters["where"] = whereCondition
115+
}
116+
} else if let channels = channels {
94117
parameters["channels"] = channels
95118
}
96-
if let pushDate: Date = pushDate {
119+
if let pushDate = pushDate {
97120
parameters["push_time"] = LCDate.stringFromDate(pushDate)
98121
}
99-
if let expirationDate: Date = expirationDate {
122+
if let expirationDate = expirationDate {
100123
parameters["expiration_time"] = LCDate.stringFromDate(expirationDate)
101124
}
102-
if let expirationInterval: TimeInterval = expirationInterval {
125+
if let expirationInterval = expirationInterval {
103126
if pushDate == nil {
104-
parameters["push_time"] = LCDate.stringFromDate(Date())
127+
parameters["push_time"] = LCDate().isoString
105128
}
106129
parameters["expiration_interval"] = expirationInterval
107130
}
108-
109-
let request = httpClient.request(.post, "push", parameters: parameters) { (response) in
110-
completion(LCBooleanResult(response: response))
131+
if let extraParameters = extraParameters {
132+
parameters.merge(extraParameters) { (current, _) in current }
133+
}
134+
return application.httpClient.request(
135+
.post, "push",
136+
parameters: parameters) { (response) in
137+
completion(LCBooleanResult(response: response))
111138
}
112-
113-
return request
114139
}
115-
116140
}

0 commit comments

Comments
 (0)