Skip to content

Commit 5143669

Browse files
authored
supporting badge count (#10)
* supprt badge count * update PoesTests * fix failing test
1 parent e649aa0 commit 5143669

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ OPTIONS:
3131
--bundle-identifier The bundle identifier to push to
3232
--mutable, -m Adds the mutable-content key to the payload
3333
--title, -t The title of the Push Notification
34+
--badge The number to display in a badge on your app’s icon
3435
--verbose Show extra logging for debugging purposes
3536
--help Display available options
3637
```

Sources/PoesCore/Payload.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,21 @@ import Foundation
1111
struct Payload: Codable {
1212
let aps: APS
1313

14-
init(title: String, body: String, isMutable: Bool) {
15-
aps = APS(alert: Alert(title: title, body: body), isMutable: isMutable)
14+
init(title: String, body: String, isMutable: Bool, badge: Int? = nil) {
15+
aps = APS(alert: Alert(title: title, body: body), isMutable: isMutable, badge: badge)
1616
}
1717
}
1818

1919
struct APS: Codable {
2020
enum CodingKeys: String, CodingKey {
2121
case alert
2222
case isMutable = "mutable-content"
23+
case badge = "badge"
2324
}
2425

2526
let alert: Alert
2627
let isMutable: Bool
28+
let badge: Int?
2729
}
2830

2931
struct Alert: Codable {

Sources/PoesCore/PayloadFactory.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,21 @@ struct PayloadFactory {
1313
private let titleArgument: OptionArgument<String>
1414
private let bodyArgument: OptionArgument<String>
1515
private let isMutableArgument: OptionArgument<Bool>
16+
private let badgeArgument: OptionArgument<Int>
1617

1718
init(parser: ArgumentParser) {
1819
titleArgument = parser.add(option: "--title", shortName: "-t", kind: String.self, usage: "The title of the Push Notification")
1920
bodyArgument = parser.add(option: "--body", shortName: "-b", kind: String.self, usage: "The body of the Push Notification")
2021
isMutableArgument = parser.add(option: "--mutable", shortName: "-m", kind: Bool.self, usage: "Adds the mutable-content key to the payload")
22+
badgeArgument = parser.add(option: "--badge", kind: Int.self, usage: "The number to display in a badge on your app’s icon")
2123
}
2224

2325
func make(using arguments: ArgumentParser.Result) -> Payload {
2426
let title = arguments.get(titleArgument) ?? "Default title"
2527
let body = arguments.get(bodyArgument) ?? "Default body"
2628
let isMutable = arguments.get(isMutableArgument) ?? false
29+
let badge = arguments.get(badgeArgument)
2730

28-
return Payload(title: title, body: body, isMutable: isMutable)
31+
return Payload(title: title, body: body, isMutable: isMutable, badge: badge)
2932
}
3033
}

Tests/PoesTests/PoesTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ final class PoesTests: XCTestCase {
2222
let expectedBundleIdentifier = "com.example.app"
2323
let title = "Notification title"
2424
let body = "Notification body"
25+
let badge = 2
2526
let isMutable = true
26-
try Poes.run(arguments: ["poes", "--bundle-identifier", expectedBundleIdentifier, "-t", title, "-b", body, "-m"])
27+
try Poes.run(arguments: ["poes", "--bundle-identifier", expectedBundleIdentifier, "-t", title, "-b", body, "-m", "--badge", "\(badge)"])
2728

2829
let command = try XCTUnwrap(MockedShell.executedCommand)
2930

@@ -40,6 +41,7 @@ final class PoesTests: XCTestCase {
4041
XCTAssertEqual(payload.aps.alert.title, title)
4142
XCTAssertEqual(payload.aps.alert.body, body)
4243
XCTAssertEqual(payload.aps.isMutable, isMutable)
44+
XCTAssertEqual(payload.aps.badge, badge)
4345

4446
}
4547
}

0 commit comments

Comments
 (0)